| View previous topic :: View next topic |
| Author |
Message |
gary Guest
|
Posted: Thu Oct 20, 2005 1:57 pm Post subject: why sizeof(string) = 4? |
|
|
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
|
Posted: Thu Oct 20, 2005 6:47 pm Post subject: Re: why sizeof(string) = 4? |
|
|
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
|
Posted: Thu Oct 20, 2005 7:34 pm Post subject: Re: why sizeof(string) = 4? |
|
|
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
|
Posted: Fri Oct 21, 2005 8:16 am Post subject: Re: why sizeof(string) = 4? |
|
|
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
|
Posted: Fri Oct 21, 2005 9:11 am Post subject: Re: why sizeof(string) = 4? |
|
|
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
|
Posted: Fri Oct 21, 2005 9:14 am Post subject: Re: why sizeof(string) = 4? |
|
|
| 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
|
Posted: Fri Oct 21, 2005 3:53 pm Post subject: Re: why sizeof(string) = 4? |
|
|
"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  |
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
|
Posted: Fri Oct 21, 2005 4:19 pm Post subject: Re: why sizeof(string) = 4? |
|
|
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
|
Posted: Fri Oct 21, 2005 10:09 pm Post subject: Re: why sizeof(string) = 4? |
|
|
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 |
|
 |
|