473,898 Members | 3,068 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 13604

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
7027
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
7760
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
25506
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
15964
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
17131
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
8416
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
4303
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
2871
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
4579
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
11259
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10857
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
10483
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
9661
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
8035
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
7187
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5880
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6076
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3305
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.