J
jl_post
Dear C++ community,
I have a question regarding the size of C++ std::strings.
Basically, I compiled the following code under two different compilers:
std::string someString = "Hello, world!";
int size1 = sizeof(std::string);
int size2 = sizeof(someString);
and printed out the values of size1 and size2. size1 and size2 always
matched in value (in other words, size1 == size2). That makes sense to
me.
Under the Visual C++ 6.0, size1 and size2 both equalled 16, but
under a GNU C++ compiler (under Linux), size1 and size2 were both 4. I
understand that different compilers are allowed to implement
std::string differently which allows for the differences between the
results of sizeof(std::string) by the different compilers.
What I don't understand is why sizeof(std::string) returns 4 with
any compiler. I mean, a value of 4 just seems too small for me. I
figure that any std::string implementation should have at least a
pointer (which points to the main string), an integer storing the
already allocated space for the main string (whose value gets returned
in the call to std::string::capacity()), and possibly even an integer
storing the length of the string.
Just the pointer alone would take up 4 bytes (I tested it and
sizeof(char*) does indeed equal 4), so I can't see how there could
possibly be any more room for anything else, like the integer that
holds the already allocated space (the one used in
std::string::capacity()). The fact that Visual C++ has a
sizeof(std::string) of 16 makes a lot more sense to me, as it clearly
has enough space to hold these integers.
So my main question is: Assuming that sizeof(char*) equals 4, how
is it possible that sizeof(std::string) can be 4 on any compiler?
Also, shouldn't sizeof(std::string) be AT LEAST sizeof(char*) +
sizeof(unsigned int) ? I'm curious why it isn't on the GNU C++
compiler that I'm using.
Thank-you in advance for any responses.
-- Jean-Luc
I have a question regarding the size of C++ std::strings.
Basically, I compiled the following code under two different compilers:
std::string someString = "Hello, world!";
int size1 = sizeof(std::string);
int size2 = sizeof(someString);
and printed out the values of size1 and size2. size1 and size2 always
matched in value (in other words, size1 == size2). That makes sense to
me.
Under the Visual C++ 6.0, size1 and size2 both equalled 16, but
under a GNU C++ compiler (under Linux), size1 and size2 were both 4. I
understand that different compilers are allowed to implement
std::string differently which allows for the differences between the
results of sizeof(std::string) by the different compilers.
What I don't understand is why sizeof(std::string) returns 4 with
any compiler. I mean, a value of 4 just seems too small for me. I
figure that any std::string implementation should have at least a
pointer (which points to the main string), an integer storing the
already allocated space for the main string (whose value gets returned
in the call to std::string::capacity()), and possibly even an integer
storing the length of the string.
Just the pointer alone would take up 4 bytes (I tested it and
sizeof(char*) does indeed equal 4), so I can't see how there could
possibly be any more room for anything else, like the integer that
holds the already allocated space (the one used in
std::string::capacity()). The fact that Visual C++ has a
sizeof(std::string) of 16 makes a lot more sense to me, as it clearly
has enough space to hold these integers.
So my main question is: Assuming that sizeof(char*) equals 4, how
is it possible that sizeof(std::string) can be 4 on any compiler?
Also, shouldn't sizeof(std::string) be AT LEAST sizeof(char*) +
sizeof(unsigned int) ? I'm curious why it isn't on the GNU C++
compiler that I'm using.
Thank-you in advance for any responses.
-- Jean-Luc