Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   pass string by value (http://www.velocityreviews.com/forums/t806819-pass-string-by-value.html)

Krzysztof Poc 12-11-2011 11:04 AM

pass string by value
 
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.

Juha Nieminen 12-11-2011 11:43 AM

Re: pass string by value
 
Krzysztof Poc <fajfusio@wp.pl> wrote:
> 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.

Jorgen Grahn 12-11-2011 02:39 PM

Re: pass string by value
 
On Sun, 2011-12-11, Krzysztof Poc wrote:
> 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

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

SG 12-11-2011 04:53 PM

Re: pass string by value
 
On Dec 11, 12:43*pm, Juha Nieminen wrote:
> [...] 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

Ebenezer 12-11-2011 10:04 PM

Re: pass string by value
 
On Dec 11, 8:39*am, Jorgen Grahn <grahn+n...@snipabacken.se> wrote:
> On Sun, 2011-12-11, Krzysztof Poc wrote:
> > 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.


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


All times are GMT. The time now is 01:08 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.