0
#include <iostream>
#include <string>
#include <cstring>

int main()
{
    using namespace std;
    string cppString = "string1";

    //size of C++ String
    cout << sizeof(cppString) << endl;
    cout << cppString << endl;

    //To list the corresponding ASCII code for C++ String
    for (int index = 0; index < sizeof(cppString); ++index)
            std::cout << static_cast<int>(cppString[index]) << " ";

    char cString[] = "string2";

    //size of C String
    cout << "\n" << sizeof(cString) << endl;
    cout << cString << endl;

    //To list the corresponding ASCII code for C String
    for (int index = 0; index < sizeof(cString); ++index)
            std::cout << static_cast<int>(cString[index]) << " ";
}

Output:

24
string1
115 116 114 105 110 103 49 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
8
string2
115 116 114 105 110 103 50 0 

I am very new to programming. I've tried googling the answer for this, but the explanation available was beyond my scope of understanding.

I am aware of that C String is what is accompanied by null terminator when created and C++ string is not. However, when I test out which ASCII code is saved in memory for both C++ String and C String, it appears that not only does C-String has the null terminator(as expected), but C++ string also ends up with continuous null terminators after ASCII code 49.

In Gaddis book, it stipulates, All of a program’s string literals are stored in memory as C-strings, with the null terminator automatically appended.

Question:

  • Following the information of Gaddis book, could it have been that my string literal, "string1", was created as C-String with null terminator at the end, rendering the cppString to behave like C-String, hence the reason why the ASCII code showing those null terminators after 49?
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
lostsoul
  • 61
  • 6
  • 3
    `sizeof(cppString)` should be `cppString.length()`. – John Kugelman Nov 07 '16 at 03:45
  • 2
    everywhere you use `sizeof(cppString)`, it should be `cppString.size()`. Make that change to your program and try again. Your code for `cppString` causes undefined behaviour by reading past the index of the last character, the `0 0 0` is undefined behaviour side-effects and not indicative of any memory layout – M.M Nov 07 '16 at 03:45
  • the cstring should use `strlen(cString)` as well. `sizeof` includes the null terminator, but `strlen` wont. – pat Nov 07 '16 at 03:50
  • @JohnKugelman Is the reason why sizeof operator won't work on cppString because sizeof only works on data type and cppString is not a data type, but rather an instantiation of string class? – lostsoul Nov 07 '16 at 05:47
  • `sizeof` gives you the number of bytes an object takes in memory. You want the number of characters in the string. Those are different numbers. `sizeof` is a compile-time constant; it doesn't change at runtime. If `string` were `class string { char *data; unsigned length; };`, then `sizeof(stringVar)` would be some fixed value: say, 8. 4 bytes for the `char *` pointer and 4 bytes for the length. No matter the length of the string, `sizeof` would always be 8. – John Kugelman Nov 07 '16 at 15:02
  • @JohnKugelman I don't fully understand your explanation as I do not have the full knowledge of the language yet, but the gist of your explanation could be that since 'sizeof' is a compile-time constant so it won't work on 'string class' since 'string class' is linked at run-time? As opposed to 'string class', using 'sizeof' on C-type string will work since character array is saved in memory at compile time? – lostsoul Nov 08 '16 at 01:40
  • Basic rule of thumb: for char arrays, use `strlen(str)`; for C++ string objects, use `str.length()`. It's rare that `sizeof(str)` will be correct. That only works on arrays with known lengths. It won't work on `char *str` style strings, which are incredibly common. And even when it does work, it includes the `'\0'` char, which is not usually what you want. – John Kugelman Nov 08 '16 at 02:02
  • @JohnKugelman Got it. Thank you so much for your explanation. I really appreciate it! – lostsoul Nov 08 '16 at 03:41

0 Answers0