Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > pass string by value

Reply
Thread Tools

pass string by value

 
 
Krzysztof Poc
Guest
Posts: n/a
 
      12-11-2011
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.
 
Reply With Quote
 
 
 
 
Juha Nieminen
Guest
Posts: n/a
 
      12-11-2011
Krzysztof Poc <> 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.
 
Reply With Quote
 
 
 
 
Jorgen Grahn
Guest
Posts: n/a
 
      12-11-2011
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 .
 
Reply With Quote
 
SG
Guest
Posts: n/a
 
      12-11-2011
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
 
Reply With Quote
 
Ebenezer
Guest
Posts: n/a
 
      12-11-2011
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Variable Input on procedure - pass by value or pass by reference? Tricky VHDL 0 03-19-2009 03:59 PM
Variable Input on procedure - pass by value or pass by reference? Tricky VHDL 0 03-19-2009 03:58 PM
pass by value or pass by address? Vols C++ 3 04-27-2008 11:40 PM
Problem understanding pass by value and pass by reference of arrays and what happens in the memory venkatagmail C++ 11 10-03-2007 02:00 PM
Pass by reference / pass by value Jerry Java 20 09-09-2005 06:08 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57