Does std::string do a shallow copy in copy construtor and assign operator ?

Discussion in 'C++' started by suman.nandan@gmail.com, May 26, 2007.

  1. Guest

    Hi Experts,
    In the following code (sorry for using C printf in the code !) :
    ----------------------------------------------
    #include <string>
    #include<cstdio>

    using namespace std;

    int main ()
    {
    string a1 = "abcd";
    string a2;

    a2 = a1;

    printf("Address of a1.c_str() is = %u\n", a1.c_str());
    printf("Address of a2.c_str() is = %u\n", a2.c_str());
    }
    ---------------------------------------------
    The output I am getting is :
    Address of a1.c_str() is = 159920148
    Address of a2.c_str() is = 159920148
    ----------------------------------------------
    I seems that the copy happening here is a shallow one.
    I have also tried with copy constructor.
    This seems to be counter intuitive. Is this expected ?

    Thanks,
    Suman.
     
    , May 26, 2007
    #1
    1. Advertising

  2. Ian Collins Guest

    Re: Does std::string do a shallow copy in copy construtor and assignoperator ?

    wrote:
    > Hi Experts,
    > In the following code (sorry for using C printf in the code !) :
    > ----------------------------------------------
    > #include <string>
    > #include<cstdio>
    >
    > using namespace std;
    >
    > int main ()
    > {
    > string a1 = "abcd";
    > string a2;
    >
    > a2 = a1;
    >
    > printf("Address of a1.c_str() is = %u\n", a1.c_str());
    > printf("Address of a2.c_str() is = %u\n", a2.c_str());
    > }
    > ---------------------------------------------
    > The output I am getting is :
    > Address of a1.c_str() is = 159920148
    > Address of a2.c_str() is = 159920148
    > ----------------------------------------------
    > I seems that the copy happening here is a shallow one.
    > I have also tried with copy constructor.
    > This seems to be counter intuitive. Is this expected ?
    >

    Yes, the there is no need to copy the string data unless either object
    is modified, so your std::string probably uses a reference counter for
    the raw data. Try modifying either string and see what you get.

    --
    Ian Collins.
     
    Ian Collins, May 26, 2007
    #2
    1. Advertising

  3. Re: Does std::string do a shallow copy in copy construtor and assignoperator ?

    wrote:
    > Hi Experts,
    > In the following code (sorry for using C printf in the code !) :
    > ----------------------------------------------
    > #include <string>
    > #include<cstdio>
    >
    > using namespace std;
    >
    > int main ()
    > {
    > string a1 = "abcd";
    > string a2;
    >
    > a2 = a1;
    >
    > printf("Address of a1.c_str() is = %u\n", a1.c_str());
    > printf("Address of a2.c_str() is = %u\n", a2.c_str());
    > }
    > ---------------------------------------------
    > The output I am getting is :
    > Address of a1.c_str() is = 159920148
    > Address of a2.c_str() is = 159920148
    > ----------------------------------------------
    > I seems that the copy happening here is a shallow one.
    > I have also tried with copy constructor.
    > This seems to be counter intuitive. Is this expected ?
    >
    > Thanks,
    > Suman.
    >


    Probably string is delaying the copy until you modify one of the
    strings. String does *not* do shallow copies, but when the copy happens
    is another matter.

    Try googling for 'copy on write'

    john
     
    John Harrison, May 26, 2007
    #3
    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. VisionSet
    Replies:
    8
    Views:
    4,902
    Tris Orendorff
    Apr 29, 2004
  2. Alex
    Replies:
    2
    Views:
    1,233
  3. Replies:
    1
    Views:
    544
    Victor Bazarov
    Jun 24, 2005
  4. Replies:
    26
    Views:
    2,119
    Roland Pibinger
    Sep 1, 2006
  5. shuisheng
    Replies:
    4
    Views:
    838
    Uenal S. Mutlu
    Dec 17, 2006
Loading...

Share This Page