K
Krzysztof Poc
Hi
I found that std::string keeps its string buffer on a heap. When a
string
object is passed to a function by value then the new string object is
allocated but it still points to the same string buffer allocated on a
heap. sizeof(string) shows 4 (on a 32 bit architecture). I conclude
its only
member variable is a pointer to a string buffer stored on a heap.
I use linux with gcc 4.4.5.
From all above I conclude that the most reasonable way to pass a
string is by
value (not a reference or pointer).
Is my understanding correct or maybe it is correct only on my
architecture.
By the way it looks like every invocation of string::length() causes
the
scanning of a string. I conclude it from sizeof(string)==4 (keeps
address only,
no string length).
Furthermore I wonder how the usage counter of a string is implemented
if a string
object takes 4 bytes only.
I found that std::string keeps its string buffer on a heap. When a
string
object is passed to a function by value then the new string object is
allocated but it still points to the same string buffer allocated on a
heap. sizeof(string) shows 4 (on a 32 bit architecture). I conclude
its only
member variable is a pointer to a string buffer stored on a heap.
I use linux with gcc 4.4.5.
From all above I conclude that the most reasonable way to pass a
string is by
value (not a reference or pointer).
Is my understanding correct or maybe it is correct only on my
architecture.
By the way it looks like every invocation of string::length() causes
the
scanning of a string. I conclude it from sizeof(string)==4 (keeps
address only,
no string length).
Furthermore I wonder how the usage counter of a string is implemented
if a string
object takes 4 bytes only.