Re: References take more menory than pointers?
Dave Rudolf <dave.rudolf@usask.ca> wrote:
Ah, I see now. When I do sizeof( std::string ) in unix, it
coincidently gives 4 as the size. Which begs the question, why do
std::string objects need more memory in Windows land than in other
systems?
It's not a Windows vs Unix issue, it's just an implementation detail of
a particular STL implementation. A 4-byte-large std::string probably
holds a pointer to a heap-allocated data structure that, in turn,
maintains the pointer to the actual data and its length. That is, the
object is not really smaller, it's just split into more parts.
I know that there can be platform differences and such, but
if a string is just an array of bytes (or whatever underlying
character type is given to the basic_string template), then why would
the class take more room in VC than in, say g++?
Again, it's not a difference between compilers, it's a difference
between STL implementations. Dinkumware STL (one that ships with MSVC)
chooses to implement so called small string optimization, where strings
up to certain lengths are stored inside the class instance itself (thus
avoiding a heap allocation), while longer strings are allocated on the
heap. STL implementation shipped with g++ apparently doesn't do that.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925