pass string by value

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.
 
J

Juha Nieminen

Krzysztof Poc said:
From all above I conclude that the most reasonable way to pass a
string is by
value (not a reference or pointer).

*Some* std::string implementations use copy-on-write (in other words,
the string data is deep-copied only if more than one std::string object
points to the same data, and one of them wants to modify it). However,
not all of them do. Hence if you want your program to be efficient in
all systems with all compilers, it's better to pass by reference whenever
possible. (Besides, even if it uses copy-on-write, updating the reference
counter is slower than simply passing a reference, as the counter update
is an additional operation on top of passing the parameter by value.)

(The downside of copy-on-write is that it makes modifying operations
slightly slower because they all need a conditional to check if the string
data is being shared.)
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).

You conclude wrongly. Just because the std::string object might contain
one pointer doesn't mean that pointer points to the char data. It probably
points to a structure which has the string length (and possible reference
count) at the beginning of the char data.
 
J

Jorgen Grahn

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. ....
From all above I conclude that the most reasonable way to pass a
string is by value (not a reference or pointer).

Why?

I use this rule: if I can choose between pass-by-value and passing a
const reference, I only pass by value if the object is obviously
small, a builtin type like an int or a pointer.

There is no large category of classes in C++ which are guaranteed to
use copy-on-write. It is not a very common design, IME.

/Jorgen
 
S

SG

[...] Just because the std::string object might contain
one pointer doesn't mean that pointer points to the char data. It probably
points to a structure which has the string length (and possible reference
count) at the beginning of the char data.

In the libstdc++ implementation it actually points to the first
character of the character string. Three values are immediately
preceeding this string: size, capacity and reference count (in some
order which I don't recall). The nice property with this approach is
that the debugger will follow the pointer member to display the string
value.

Anyhow... @OP: Try not to rely on implementation details. Otherwise
your code won't be portable or it even might break in the future in
case the standard library implementation changes...

Cheers!
SG
 
E

Ebenezer

Why?

I use this rule: if I can choose between pass-by-value and passing a
const reference, I only pass by value if the object is obviously
small, a builtin type like an int or a pointer.

This is what I use also.
There is no large category of classes in C++ which are guaranteed to
use copy-on-write. It is not a very common design, IME.

Agreed.

Brian Wood
Ebenezer Enterprises
http://webEbenezer.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top