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

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

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

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:
    5,230
    Tris Orendorff
    Apr 29, 2004
  2. Matthias =?ISO-8859-1?Q?K=E4ppler?=

    std::string::push_back vs. std::string::operator+=

    Matthias =?ISO-8859-1?Q?K=E4ppler?=, Nov 22, 2004, in forum: C++
    Replies:
    2
    Views:
    4,494
    Jonathan Mcdougall
    Nov 23, 2004
  3. Alex
    Replies:
    2
    Views:
    1,634
  4. Replies:
    1
    Views:
    693
    Victor Bazarov
    Jun 24, 2005
  5. Geoffrey S. Knauth
    Replies:
    6
    Views:
    1,309
    Earl Purple
    Jan 18, 2006
  6. Replies:
    26
    Views:
    2,609
    Roland Pibinger
    Sep 1, 2006
  7. shuisheng
    Replies:
    4
    Views:
    1,015
    Uenal S. Mutlu
    Dec 17, 2006
  8. Martin T.
    Replies:
    7
    Views:
    996
    Martin T.
    Mar 10, 2008
Loading...