pass string by value

Discussion in 'C++' started by Krzysztof Poc, Dec 11, 2011.

  1. 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.
    Krzysztof Poc, Dec 11, 2011
    #1
    1. Advertising

  2. 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.
    Juha Nieminen, Dec 11, 2011
    #2
    1. Advertising

  3. Krzysztof Poc

    Jorgen Grahn Guest

    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 .
    Jorgen Grahn, Dec 11, 2011
    #3
  4. Krzysztof Poc

    SG Guest

    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
    SG, Dec 11, 2011
    #4
  5. Krzysztof Poc

    Ebenezer Guest

    On Dec 11, 8:39 am, Jorgen Grahn <> 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
    Ebenezer, Dec 11, 2011
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jerry
    Replies:
    20
    Views:
    7,891
    Roedy Green
    Sep 9, 2005
  2. venkatagmail
    Replies:
    11
    Views:
    666
    James Kanze
    Oct 3, 2007
  3. Vols
    Replies:
    3
    Views:
    359
    Ian Collins
    Apr 28, 2008
  4. Tricky
    Replies:
    0
    Views:
    559
    Tricky
    Mar 19, 2009
  5. Tricky
    Replies:
    0
    Views:
    976
    Tricky
    Mar 19, 2009
Loading...

Share This Page