473,657 Members | 2,667 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sizeof(std::str ing) seems to small

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::str ing);
int size2 = sizeof(someStri ng);

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::str ing) by the different compilers.

What I don't understand is why sizeof(std::str ing) 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::ca pacity()), 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::ca pacity()). The fact that Visual C++ has a
sizeof(std::str ing) 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::str ing) can be 4 on any compiler?

Also, shouldn't sizeof(std::str ing) 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

Sep 28 '05 #1
12 13571

jl*****@hotmail .com wrote:
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::str ing);
int size2 = sizeof(someStri ng);

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::str ing) by the different compilers.

What I don't understand is why sizeof(std::str ing) 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::ca pacity()), 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::ca pacity()). The fact that Visual C++ has a
sizeof(std::str ing) 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::str ing) can be 4 on any compiler?

Also, shouldn't sizeof(std::str ing) 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


The library could implement it using the Pimpl idiom (cf.
http://www.gotw.ca/gotw/024.htm):

class stringImpl;

class string
{
public:
// Forwarding functions
private:
stringImpl* pImpl;
};

Thus you have only a pointer as a member.

Cheers! --M

Sep 28 '05 #2
jl*****@hotmail .com wrote:
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::str ing);
int size2 = sizeof(someStri ng);

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::str ing) by the different compilers.

What I don't understand is why sizeof(std::str ing) returns 4 with
any compiler.
It does? Really? Wait, didn't you just say that "Under the Visual C++
6.0, size1 .. equalled 16"? And 'size1' _is_ 'sizeof(std::st ring)', no?
So, why do you say "sizeof(std::st ring) returns 4 with any compiler"? It
apparently does NOT in VC++ 6.0...
[...]

So my main question is: Assuming that sizeof(char*) equals 4, how
is it possible that sizeof(std::str ing) can be 4 on any compiler?
It isn't.
Also, shouldn't sizeof(std::str ing) be AT LEAST sizeof(char*) +
sizeof(unsigned int) ? I'm curious why it isn't on the GNU C++
compiler that I'm using.


"Use the Source, Luke!" Just look at their implementation. They
may have a simple thing like

class blah {
blah_internal *pimpl;
public:
/// all members simply forwarding the requests to 'pimpl'
};

V
Sep 28 '05 #3
Any type in C++ has a fixed value. This is because the compiler needs to
know the exact size of each type to allocate stack frame.

An std::string object does not contain the string data in the object itself
typically. Rather, it dynamically manages the string content somewhere else
in the memory. Usually, and by default, it allocates/ manages/ and
eventually deallocates the string content on the free store.

An std::string only needs a pointer to the content and an integer to cache
the size of the string. Of course, more complex of representation is
possible. Many versions of std::string manages its dynamic string contents
as a number of memory "chunks".

Ben
Sep 28 '05 #4

jl*****@hotmail .com wrote:
So my main question is: Assuming that sizeof(char*) equals 4, how
is it possible that sizeof(std::str ing) can be 4 on any compiler?
Then, if std::string only contains a char* then it's size is 4.
so I can't see how there could possibly be any more room for anything else
Also, shouldn't sizeof(std::str ing) be AT LEAST sizeof(char*) +
sizeof(unsigned int) ?


It could be implemented so that they're using some of the first few
bytes in the memory pointed to by this char * for something other than
the characters of the string. Then you only need one char *, you don't
need to store anything else as members of the object. Then,
std::string::c_ str() could return this char * + (some number to skip
the non character data)*sizeof(ch ar), std::string::ca pacity could
return *((int*)( pointer to char* + capacity location )),... I'm just
speculating, but this is at least one way that it could be implemented
with a single char *.

-Brian

Sep 28 '05 #5
jl*****@hotmail .com wrote:

So my main question is: Assuming that sizeof(char*) equals 4, how
is it possible that sizeof(std::str ing) can be 4 on any compiler?
I really don't know how the GNU people implemented std::string.
But who says that the pointer in std::string has to point to the
characters?
What about an intermeidate structure which holds, amongst the other
things you mentioned, a reference counter? Then it would be possible
that in

std::string st1 = "hello world";
std::string st2 = "hello world";

both strings internally point to the very same memory area

st1 st2
+-------+ +--------+
| o-----------+ +-------------o |
+-------+ | | +--------+
| |
| |
v v
+----------+
| cap: 12 |
| len: 11 |
| ref: 2 |
| data: o--------+
+----------+ |
|
+-------------------------+
|
v
+---+---+---+---+---+---+---+---+---+---+---+---+
| h | e | l | l | o | | w | o | r | l | d | |
+---+---+---+---+---+---+---+---+---+---+---+---+
Also, shouldn't sizeof(std::str ing) be AT LEAST sizeof(char*) +
sizeof(unsigned int) ? I'm curious why it isn't on the GNU C++
compiler that I'm using.


As said: I don't know if the GNU people did it that way. But it would
be possible.

--
Karl Heinz Buchegger
kb******@gascad .at
Sep 28 '05 #6
Victor Bazarov <v.********@com Acast.net> wrote in news:jsy_e.3681 3$Tf5.5443
@newsread1.mlps ca01.us.to.veri o.net:
What I don't understand is why sizeof(std::str ing) returns 4 with
any compiler.


It does? Really? Wait, didn't you just say that "Under the Visual C++
6.0, size1 .. equalled 16"? And 'size1' _is_ 'sizeof(std::st ring)', no?
So, why do you say "sizeof(std::st ring) returns 4 with any compiler"? It
apparently does NOT in VC++ 6.0...


Uh, Victor... the OP is expressing his surprise that there exists at least
one compiler for which sizeof(std::str ing) is 4, not that every compiler
returns 4... (existential vs. universal quantifier....)
Sep 28 '05 #7
> jl*****@hotmail .com wrote:

What I don't understand is why sizeof(std::str ing)
returns 4 with any compiler.

Victor Bazarov replied:
It does? Really? Wait, didn't you just say that
"Under the Visual C++ 6.0, size1 .. equalled 16"?
And 'size1' _is_ 'sizeof(std::st ring)', no?
So, why do you say "sizeof(std::st ring) returns 4
with any compiler"? It apparently does NOT in VC++
6.0...

I apologize, Victor. When I said "it returns 4 with any compiler" I
did not mean "it returns 4 with EVERY compiler." By using the word
"any" I meant to say that "if there exists any compiler with which
sizeof(std::str ing) returns 4, then I have trouble understanding why 4
is returned."

And you are right in saying that it apparently does not return 4 in
VC++ 6.0. My point was that it made sense to me that VC++ returned a
value greater than 4, but I was confused that some compilers (by which
I mean GNU C++ and not VC++ 6.0) returned 4.

By using the word "any," I didn't mean "every."

Sorry for the misunderstandin g, Victor.

-- Jean-Luc

Sep 28 '05 #8
Andre Kostur wrote:
Victor Bazarov <v.********@com Acast.net> wrote in news:jsy_e.3681 3$Tf5.5443
@newsread1.mlps ca01.us.to.veri o.net:

What I don't understand is why sizeof(std::str ing) returns 4 with
any compiler.


It does? Really? Wait, didn't you just say that "Under the Visual C++
6.0, size1 .. equalled 16"? And 'size1' _is_ 'sizeof(std::st ring)', no?
So, why do you say "sizeof(std::st ring) returns 4 with any compiler"? It
apparently does NOT in VC++ 6.0...

Uh, Victor... the OP is expressing his surprise that there exists at least
one compiler for which sizeof(std::str ing) is 4, not that every compiler
returns 4... (existential vs. universal quantifier....)


My apologies. English is not my native tongue, I sometimes have trouble
with it.

V
Sep 28 '05 #9
In article <43************ ***@gascad.at>,
Karl Heinz Buchegger <kb******@gasca d.at> wrote:
std::string st1 = "hello world";
std::string st2 = "hello world";

both strings internally point to the very same memory area

st1 st2
+-------+ +--------+
| o-----------+ +-------------o |
+-------+ | | +--------+
| |
| |
v v
+----------+
| cap: 12 |
| len: 11 |
| ref: 2 |
| data: o--------+
+----------+ |
|
+-------------------------+
|
v
+---+---+---+---+---+---+---+---+---+---+---+---+
| h | e | l | l | o | | w | o | r | l | d | |
+---+---+---+---+---+---+---+---+---+---+---+---+


Nice ASCII art! :-)

Just fyi, "Effect STL" by Scott Meyers does a nice survey std::string
layouts circa 2000 (Item 15). Things have changed since then in at
least one implementation I'm aware of (CodeWarrior) but it is still a
nice survey.

The above diagram is very close to "Implementa tion B" from this survey.
"Implementa tion C" from the survey gives an example where sizeof(string)
would be equal to sizeof(char*).

-Howard
Sep 28 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
7017
by: karthik.naig | last post by:
Hi, This was the routine I wrote earlier to convert a C++ string to a char array. But I found that the char* array consisted only of junk after returning from the below function.
4
7741
by: vivekian | last post by:
Hi, This is the part of the code am trying to compile to : void Server::respondToClient ( std::string response ) { .... .... if ((numbytes = sendto ( sockFd_ , response , sizeof(response) , 0, (struct sockaddr *)&clientAddr, sizeof (clientAddr))) == -1){
10
25478
by: farseer | last post by:
How can i do this? i'd like to call the following code: .... string url = <my urld>; TCHAR* urlParams = GetParams( ); url.append( (char * ) urlParams ); GotoURL( ( LPCTSTR ) url ); <------THIS IS MY ISSUE ....
84
15842
by: Peter Olcott | last post by:
Is there anyway of doing this besides making my own string from scratch? union AnyType { std::string String; double Number; };
4
16887
by: hugob0ss | last post by:
Hi, i'm with a problem here that i can't understand what it is. Hi have this code struct SF { std::string mnemonic;//mnemonic that represents it std::string name;//a descriptive name ushort num_val1;// number of the first value uchar possiblevalues_num; //number of the possible values. uchar type; /* = 1 if the feature is utilized in the models*/
25
8339
by: Bala2508 | last post by:
Hi, I have a C++ application that extensively uses std::string and std::ostringstream in somewhat similar manner as below std::string msgHeader; msgHeader = "<"; msgHeader += a; msgHeader += "><";
11
4257
by: Christopher Pisz | last post by:
Is std::string::npos always going to be less than any std::string 's size()? I am trying to handle a replacement of all occurances of a substr, in which the replacement also contains the substr. Yick. All I could come up with is: #include <string> int main() { std::string text;
5
2849
by: scudemax | last post by:
I am having trouble porting some code from VC6 to VC8 (2005). I need to deserialize files that were written in VC6 and heavily use the std::string template. sizeof(std::string) will be 16 from vc6, but will be 32 from vc8. I need it to be 16. see the sample code below: CODE #include <string> int main(int argc, char* argv)
5
4551
by: Ramesh | last post by:
Hi. Assuming I have a code snippet like below: #include <iostream> using namespace std; char Mac = { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5 }; std::string csMac;
0
8402
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8315
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8734
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8608
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7341
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6172
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4323
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2733
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1962
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.