0

I have been just reading the book The C++ Programming Language by Bjarne Stroustrup. On the page 105, I tried the exercise 12 and I came to one (for me) crazy thing. After this, I managed to successfully solve the exercise, but I am interested, how and why does this happen? Here is the minimized code (only with this specific case):

int main()
{
    char c;
    std::cin >> c;
    std::cout << "" + c;
}

Try to input one character and the result is pretty surprising. For 'a', I get Community\VC\Tools\MSVC\14.28.29910\include\vector, and for 'Z' I get o\2019\Community\VC\Tools\MSVC\14.28.29910\include\vector. This applies to other characters I try to input as well. I really can't find any pattern in this. What does the vector directory have to do with the inputed character?

I know that such adding string with character is forbidden (adding different entities) and there are many alternatives I used afterwards, but I am interested: Why does this actually happen? Where does the Visual Studio get some random directory, slice it, and serve it in the console?


As of comments, I now know that I actually added together the address of the string literal and the numerical value of the character. But now I am interested: why are the addresses of string literals always the same? Consider the following code:

int main()
{
    char c;
    std::cin >> c;
    std::cout << "" + c << std::endl << &"" << std::endl << &"Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.28.29910/include/vector";
}

The addresses of the string literals "" and "Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.28.29910/include/vector" (the longest string I got) are always the same. Why? Isn't the memory address given randomly?

User123
  • 476
  • 7
  • 22
  • 4
    Undefined behaviour is seldom predictable! You are adding an *integer* value (the code for the input character) to the address of a string literal (the `""`). That resultant address will point ***somewhere*** and, in your case, just happens to point to the string(s) you are seeing. – Adrian Mole Jul 04 '21 at 22:07
  • What @AdrianMole said.. You are adding the int value of what is read into 'c' to the address of an empty string. Unpredictable results. `""` isn't a std::string. It's the address of a C string – doug Jul 04 '21 at 22:09
  • @AdrianMole Interesting, I read in this book that such literals are saved in the so-called *read-only* part of the RAM. So, I suppose, that the directory of the vector value is saved here as well. But can you maybe explain why does the string literal act as a pointer value and not as an array of characters here in this particular case? Also, isn't the address of the literals and variables determined randomly? Why do I get always the same string with same characters then? – User123 Jul 04 '21 at 22:10
  • It *is* an array (of one character - the `nul` character). That array decays (in many circumstances) to the address of its first element. – Adrian Mole Jul 04 '21 at 22:11
  • 1
    `""` is a C type string that is just the terminating null and it's value is it's address. Not it's contents. – doug Jul 04 '21 at 22:12
  • Since I added another curiosity, I voted to reopen the question. – User123 Jul 04 '21 at 22:27
  • "Why does this actually happen? Where does the Visual Studio get some random directory, slice it, and serve it in the console?" It's because `""` is the address of a null terminating char. To that address you are adding the integer value of the char you input. So the new address is pointing to some area in memory that just happens to have what gets interpreted as the start of a sequence of chars and that's what gets printed. I get different output and just as nonsensical. – doug Jul 04 '21 at 22:35
  • Handy reading: [String Literal](https://en.cppreference.com/w/cpp/language/string_literal) – user4581301 Jul 05 '21 at 01:50

0 Answers0