2

I am wondering how come the sizeof function returned 8 no matter the length of my input

int main(){
   string input;
   getline(cin,input);
   cout << "size of input is  " << sizeof(input) << endl; //I am guessing 
//it returns the size of a pointer because my OS is 64 bits.
   return 0;
}

So my question is that where the implicit conversion happened? here is the declaration of getline,

istream& getline ( istream& is, string& str );

Also, this sort of conversion always happen, i.e from whatever to a pointer type, is there a general case for that? Thank you.

7
  • 4
    Uh, input.size()... sizeof(input) is the size of any std::string object, not that std::string's contained data. Commented May 25, 2012 at 23:27
  • I would, I just wanna know if there was a conversion to pointer type. Thanks Commented May 25, 2012 at 23:28
  • Why would there be? Where would there be? The question is nonsensical -- sizeof(input) is certainly larger than sizeof(void*) or any other pointer type. Commented May 25, 2012 at 23:29
  • @ildjarn, I'd personally throw in a possibly explicit operator const char *() to my String class. You can get that using c_str() of course here though. Commented May 25, 2012 at 23:30
  • @chris : That wouldn't have any effect on sizeof's behavior though. Commented May 25, 2012 at 23:30

2 Answers 2

13

sizeof gives you the size in bytes. std::string contains a pointer to the actual data. The size of the single pointer stays the same no matter the size of what it's pointing to. This, combined with the other factors gives you your total size of 8.

You're looking for either std::string::size or std::string::length for the actual length of the string. If you're looking for a function to retrieve the size of any null-terminated C-String, use strlen().

Sign up to request clarification or add additional context in comments.

2 Comments

Don't those functions give the number of characters in the string? That's (in some senses at least) subtly different from the size of the string, particularly when the string isn't over char as the basic character type, though there are probably other (constant) factors too, and also the difference between the space used and the space reserved...
@DonalFellows, that's exactly what I meant. If the OP wanted the actual number of characters in the string, length() would be the right one. I'm not 100% sure if size() is the same, but for consistency with other containers, but it's always worked that way for me. I guess if you have a wide string it might make a difference, but I haven't tested/looked at it.
3

There is no conversion done here, it's simply that sizeof will return the actual byte size of the object. Obviously std::string uses heap-allocated memory to store its actual content, so you have no real way to know exactly how many bytes in total are used for a specific string

5 Comments

Thanks, that makes much more sense, I always thought there was a conversion to a pointer type, so if the input gets long enough, the return of sizeof could be bigger than 8 in theory, right? Thanks
the result of sizeof will always be the same for every string you use, for a given compiler version. It's totally unrelated to the size of the input
@ClintHui, sizeof (yourStringObject) will never change. As far as it's concerned, the only thing there is the pointer. How it uses the pointer is unrelated. The actual string you input is stored in a separate location in memory that is irrelevant to your object except for the fact that the object knows where it is and that it is responsible for it.
the thing that the string contains an internal pointer cleared my confusion. Thank you all.
It isn't necessarily true (much less "obvious") that std::string uses a heap pointer. Google "small string optimization" for a counter-example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.