By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,513 Members | 1,616 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,513 IT Pros & Developers. It's quick & easy.

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

P: n/a
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 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
su**********@gmail.com 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.
May 26 '07 #2

P: n/a
su**********@gmail.com 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
May 26 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.