Jump to content

Wikipedia:Reference desk/Archives/Computing/2021 July 9

From Wikipedia, the free encyclopedia
Computing desk
< July 8 << Jun | July | Aug >> July 10 >
Welcome to the Wikipedia Computing Reference Desk Archives
The page you are currently viewing is a transcluded archive page. While you can leave answers for any questions shown below, please ask new questions on one of the current reference desk pages.


July 9

[edit]

Section tag

[edit]

hello, please what is the function of this tool <section begin=AStandings />?--طلال (talk) 03:10, 9 July 2021 (UTC)[reply]

The code allows part of a page to be included into others. The code above marks the start of the part and there should be matching <section end=AStandings /> further down the page. See Help:Labeled section transclusionGhostInTheMachine talk to me 14:26, 9 July 2021 (UTC)[reply]

Arbitrary hard computationally problem?

[edit]

We know that there are NP-hard problem and some applications as sha-256, but for all of the applications, there is a fixed amount of computation (even if extremely large), needed to solve them. It is clear that if you take 10 different sha-256 string, the amount of computation needed to solve each of the string will be multiplied by 10 (though it can be parallelized, so in so some sense it might haven't changed at all). 1)let m be an arbitrary natural number. Can an algorithm exist, so the complexity ratio between calculating the challenge (for example hashing the string), to solving the challenge (for example find the original string) will be proportion to m without increasing the calculating the challenge complexity? I think the answer is true if we only dealing with hashing, as one my just toss a random string and define the hashed password as 1 million hashing calculation . But this leads to a different problem, the validation complexity is proportional to m. Furthermore, It is not clear, if 2) one can make an asymmetricity like in public private keys protocol. Does anyone have idea regarding this questions? --Exx8 (talk) 10:38, 9 July 2021 (UTC)[reply]

Is m meant to stand for the size of the input of a computational problem? If the effort needed to solve the problem is proportional to the size of the input, its complexity is obviously polynomial, so I think you mean something else. It is also not clear to me what "increasing the calculating the challenge complexity" means. I also have a problem understanding the meaning of "one my just toss a random string and define the hashed password as 1 million hashing calculation". (If you use Google translate to translate what you wrote into your native language, does it make sense to you?)  --Lambiam 12:16, 9 July 2021 (UTC)[reply]

1)m stand for arbitrary complexity factor. the complexity of the algorithm to solve will be O(2^mn) for example. for example in NP problems, one can verify a solution polynomially , but find one exponentially (as far we know to this date). the idea here is to add flexibility to the algorithm: that it is complexity between solving the problem will be determent arbitrarily. Meaning you will be able to make a challenge as hard as you want, not depending only the m factor. 2)"increasing the calculating the challenge complexity" means that one might just decide to harden the challenge without paying extra complexity of calculating the challenge. for example that for any n the complexity of calculating the hashing will be constant regardless the factor constant, while unhashing (calculating the matched string) will be as hard at least as (O(2^(mn)). 3)sorry me the auto correct has replaced "might" with my. is now the sentence make sense? one might just toss a random string and define the hashed password as 1 million hashing calculation--Exx8 (talk) 16:19, 10 July 2021 (UTC)[reply]

C++, Adding an int to a string

[edit]

I was very sleepy and writing code. I had a function write_file that took a string str and would write data to a file with a name "name.<str>.ext". I needed to append an integer i to str and wanted write_file(str + to_string(i)), but I wrote write_file(str + i). It ran fine, but there were no files where they should be. It occurred to me that I'm not sure what object you end up with when you add an int to a string, and, more so, why the program didn't through a fit. More importantly, did it write files somewhere weird? I'm not a programmer (math person that needs to run simulations on a bunch of data), so apologies if this is a bit weird of a post. Thank you for any help.2601:547:1:4EE0:F463:8DEA:5A4E:B34C (talk) 11:11, 9 July 2021 (UTC)[reply]

The "path" argument of  write_file  is basically a pointer to a character array. A path such as "nonprecious.txt" is actually a pointer to an array of 16 bytes (one extra because it is a null-terminated string). Adding the value 3 to the pointer means skipping the first three bytes. So write_file("nonprecious.txt" + 3, data); has the same effect as write_file("precious.txt", data);, so a precious file may be unintentionally overwritten. If the integer added exceeds the length of the string, the result of the addition is a pointer to somewhere in memory outside the array, which can contain any random data that happens to be hanging around. Check for any recently created files with strange (possibly unprintable) names.  --Lambiam 11:53, 9 July 2021 (UTC)[reply]
Thank you so much! When I tested the result by printing to console, nothing was there (the int was greater than str's length); which left me a bit worried that it was something like you describe. Though, since all files end up in the same place, the path gets attached to the start of str in write_file; so write_file(str) should write to, say, /directory/prefix.<str>.ext; so, as a follow up, if str + int is junk, would it still end up writing to somewhere in /directory and have a the ".ext" extension attached? In other words, if int is larger than the string length and I pass str + int, would this be the same as passing random junk into the write_file function?2601:547:1:4EE0:F463:8DEA:5A4E:B34C (talk) 12:33, 9 July 2021 (UTC)[reply]
I see forward slashes, so apparently you are not using Microsoft Windows. I do not know how the function write_file is implemented in the file I/O library you are using, but a decent implementation should actually start by checking that the filename (after expansion with the current path and any default extensions) is the pathname of a file that is open for writing, which a random filename should obviously not be. So it is strange that the program execution did not abort.  --Lambiam 16:36, 9 July 2021 (UTC)[reply]
In C++, a string can be either a C-style character array as Lambiam describes, or it may be a std::string. It's not clear from your description which you have, but since you were expecting the + operator to append to the string, it seems you have a std::string rather than a char array? On the other hand, adding an int to a std::string will indeed generate a compiler error, so I'm unclear what your code actually looks like. It's also not clear if write_file is a function you wrote or if it comes from a library; it would be helpful to post your actual code. In regards to your last question, appending junk to a path will generally indeed refer to a junk-named file in that directory, although there are cases where this won't happen (eg. if the junk contains a slash, it will refer to some (possibly nonexistent) subdirectory, or if the junk contains a "/../", it may write to a file in the parent directory, and so on). CodeTalker (talk) 20:22, 9 July 2021 (UTC)[reply]

Not sure if this helps, but you can define a fairly simple function to handle arbitrary type conversions:

#include <cassert>
#include <sstream>

template <typename To, typename From>
To convert(const From& from) {
  To to;
  std::stringstream io;
  assert(io << from);
  assert(io >> to);
  assert(io.tellg() == -1);
  return to;
}

Example usage:

  string A123 = "A";
  A123 += convert<string>(123);
  cout << A123 << endl; // Prints `A123`

Earl of Arundel (talk) 22:22, 9 July 2021 (UTC)[reply]