string a= "Stack Overflow";
char b[]= "Stack Overflow";
cout<<sizeof(a)<<","<<sizeof(b)<<endl;
Output of above code is 4,15
Since 'a' points to the string, it has size 4 that of a string on my machine.
Since 'a' points to the string, it has size 4 that of a string on my machine.
Not exactly.
a IS A string. It is not a pointer and, hence, does not point to a string. The implementation of string on your setup is such that sizeof(string) is 4.
'b' is also pointer to string, but why it has size of 15 (i.e. of sizeof("Stack Overflow")) ?
Not true.
b is not a pointer to a string. It is an array of char. The line
char b[]= "Stack Overflow";
is equivalent to:
char b[15]= "Stack Overflow";
The compiler deduces the size of the array and creates an array of the right size.
std::stringcan be implemented containing a pointer to a dynamically buffer containing the characters in the string and a bit of bookkeeping for the length and whatnot, but there are many ways to implement astd::stringThis one happens to pack everything it needs into 4 bytes.stringhappens to be implemented, this one is more fundamental.std::stringtypically has a size of 16 or so. It seemsstringis an alias forchar const*in this case.