C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

why sizeof(string) = 4?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
gary
Guest





PostPosted: Thu Oct 20, 2005 1:57 pm    Post subject: why sizeof(string) = 4? Reply with quote



Hi,
Who can say something about the class of string? Why sizeof(string) = 4?

Thanks.



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
liftmaster
Guest





PostPosted: Thu Oct 20, 2005 6:47 pm    Post subject: Re: why sizeof(string) = 4? Reply with quote



I am not completely sure of the question you are asking.

However sizeof gives the size of the string class (usually this is just
the sum of the sizes of the member variables). This will depend on the
implementation and potentially the compiler, platform and compiler
settings. sizeof(T*) for any T is usually 4, which suggests that the
string class you have uses a pimpl (pointer implementation). i.e. the data
structure containing for the string is 'hidden' behind a single pointer.
You will be able to find plenty of info on pimpls if you so desire.

Of course there are other explainations...


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Thomas Maeder
Guest





PostPosted: Thu Oct 20, 2005 7:34 pm    Post subject: Re: why sizeof(string) = 4? Reply with quote



gary <zwng (AT) gawab (DOT) com> writes:

Quote:
Who can say something about the class of string?

Any textbook covering the C++ Standard Library. See the reviews at
http://www.accu.org/ .


Quote:
Why sizeof(string) = 4?

Why not?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Rob
Guest





PostPosted: Fri Oct 21, 2005 8:16 am    Post subject: Re: why sizeof(string) = 4? Reply with quote

gary wrote:

Quote:
Hi,
Who can say something about the class of string? Why sizeof(string)
= 4?


The short answer is "because that's what your compiler does". sizeof
(any_type) is implementation defined.

In practice, the std::string might be implemented as a wrapper for a
pointer to char which, on 32 bit systems, often has size of 4.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Alberto Ganesh Barbati
Guest





PostPosted: Fri Oct 21, 2005 9:11 am    Post subject: Re: why sizeof(string) = 4? Reply with quote

gary wrote:
Quote:
Who can say something about the class of string? Why sizeof(string) = 4?


sizeof(std::string) is 28 on my platform (Vc++ 7.1). There's no reason
why it should be 4. In fact, it clearly isn't always like that.

Ganesh

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
jrm
Guest





PostPosted: Fri Oct 21, 2005 9:14 am    Post subject: Re: why sizeof(string) = 4? Reply with quote

Quote:
Who can say something about the class of string? Why sizeof(string) = 4?

It is implemention dependant. The one that I am familiar with is the
g++ (2x and 3x) implementation of string which closely corresponds to
'implementation C' as described in
"Effective STL - Scott Meyers" - Item 15. In this implementation, the
sizeof(string) is 4, as
it uses a 'pointer to a rep.' object which stores the 'ref. count,
capacity, and current length'.

Hope this helps.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Gene Bushuyev
Guest





PostPosted: Fri Oct 21, 2005 3:53 pm    Post subject: Re: why sizeof(string) = 4? Reply with quote

"gary" <zwng (AT) gawab (DOT) com> wrote

Quote:
Hi,
Who can say something about the class of string? Why sizeof(string) = 4?

I can say something Smile

You are probably a lucky one. In my standard library implementation
sizeof(std::string) is 28 bytes. Now, what is your question?

-- Gene
----------------------------------------------------------------
There is no greatness where there is no simplicity, goodness and truth. ~ Leo
Tolstoy


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Carl Barron
Guest





PostPosted: Fri Oct 21, 2005 4:19 pm    Post subject: Re: why sizeof(string) = 4? Reply with quote

In article <M0T5f.624$65.14745 (AT) twister1 (DOT) libero.it>, Alberto Ganesh
Barbati <AlbertoBarbati (AT) libero (DOT) it> wrote:

Quote:
gary wrote:
Who can say something about the class of string? Why sizeof(string) = 4?


sizeof(std::string) is 28 on my platform (Vc++ 7.1). There's no reason
why it should be 4. In fact, it clearly isn't always like that.

Ganesh

There might be a reason:) Std::basic_string makes few complexity

requirements so 'almost anything' that works is valid.

Perhaps basic_string determines its storage strategy depending
on the data size of the resulting string say simple array if size is
less than 256, ptr to contiquous data if size to be < 16 K or
some rope strategy otherwise, all the implementations provide
implimentations for a C *data() and a size_type size() const
virtual function of a base class say Impl. if basic string uses
deep copy strategy in various ctors assignments and dtor then
the implimentaton of basic string itself contains one pointer to
a base class.

this is how tr1::function can be implimented, I think Loki::Function
uses a Loki::SmartPtr, and boost::function use a boost::shared_ptr
but this is an implimentation detail. It is possible to use a
DeepCopyPtr as well which only need to hold a pointer, particually
for basic_string as it has 'deep copy semantics'.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Alberto Ganesh Barbati
Guest





PostPosted: Fri Oct 21, 2005 10:09 pm    Post subject: Re: why sizeof(string) = 4? Reply with quote

Carl Barron wrote:
Quote:
In article <M0T5f.624$65.14745 (AT) twister1 (DOT) libero.it>, Alberto Ganesh
Barbati <AlbertoBarbati (AT) libero (DOT) it> wrote:

sizeof(std::string) is 28 on my platform (Vc++ 7.1). There's no reason
why it should be 4. In fact, it clearly isn't always like that.

There might be a reason:) Std::basic_string makes few complexity
requirements so 'almost anything' that works is valid.

Of course. The standard gives requirements about the observed behaviour
and that usually provides a lot of freedom to the implementation.

Quote:
snip> if basic string uses
deep copy strategy in various ctors assignments and dtor then
the implimentaton of basic string itself contains one pointer to
a base class.

Not necessarily. For example VC7.1 implementation of std::string puts
very short strings directly in the std::string object as a C array
(that's why the std::string object is so big). By doing this, it saves a
dynamic allocation, which is a very expensive operation, in a large
number of cases. Long strings are still dynamic allocated as contigous
arrays. In particular, there are no pointer to bases classes in this
implementation.

Of course, this implementation might be suboptimal for certain specific
usage patterns, but I believe it's a good all-purpose implementation
nonetheless. Better than a "always dynamically allocated strings with
deep-copy semantic" implementation, in my humble opinion.

Ganesh

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.