LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 10-23-2007, 09:32 AM   #1
micropanther
LQ Newbie
 
Registered: Oct 2007
Location: Charlotte, NC
Distribution: Ubuntu 10.04, Davinci (MacOS/X)
Posts: 19

Rep: Reputation: 0
Does copying a (STL) list member of type string also copy the data?


Using the STL "list" class, I have created a list of things with type "string".

When I copy a list element to another list, is the actual data of the string also copied?

I think I understand that a string variable is a structure which (appears to) contains a pointer to the actual data and a length. Copying the string variable (structure) doesn't necessarily imply that the "pointed-to" data is also copied.

Warmly,

Micropanther (Wes Miller)
 
Old 10-23-2007, 09:48 AM   #2
krizzz
Member
 
Registered: Oct 2004
Location: NY
Distribution: Slackware
Posts: 198

Rep: Reputation: 30
Hi,

It seems that the entire object is copied, according to this manual :
http://www.sgi.com/tech/stl/copy.html
copy performs *a = *b operation internally therefore the memory pointed by the pointer is copied.

Best,
Chris
 
Old 10-23-2007, 09:49 AM   #3
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
I don't know what the standard says about this as some implementations use C.O.W.

It would seem its implementation defined
21.3 Class template basic_string
Quote:
6 [Note: These rules are formulated to allow, but not require, a reference counted implementation. A reference
counted implementation must have the same semantics as a non-reference counted implementation.
[Example:
string s1("abc");
string::iterator i = s1.begin();
string s2 = s1;
*i = ’a’; // Must modify only s1
—end example] —end note]

Last edited by dmail; 10-23-2007 at 09:57 AM.
 
Old 10-23-2007, 09:59 AM   #4
micropanther
LQ Newbie
 
Registered: Oct 2007
Location: Charlotte, NC
Distribution: Ubuntu 10.04, Davinci (MacOS/X)
Posts: 19

Original Poster
Rep: Reputation: 0
Question C.o.w. ????

Quote:
Originally Posted by dmail View Post
I don't know what the standard says about this as some implementations use C.O.W.
C.O.W. ????

Wes
 
Old 10-23-2007, 10:06 AM   #5
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
http://en.wikipedia.org/wiki/Copy-on-write
 
Old 10-23-2007, 11:22 AM   #6
krizzz
Member
 
Registered: Oct 2004
Location: NY
Distribution: Slackware
Posts: 198

Rep: Reputation: 30
I still think the *a=*b approach is used. If you have doubts just compare values of the resulting pointers form the list, they should be the same if the pointer is copied and different if the new memory is allocated.
 
Old 10-29-2007, 08:28 AM   #7
micropanther
LQ Newbie
 
Registered: Oct 2007
Location: Charlotte, NC
Distribution: Ubuntu 10.04, Davinci (MacOS/X)
Posts: 19

Original Poster
Rep: Reputation: 0
Thanks all for the help. My coworker is testing to see what he's really getting.

Wes
 
  


Reply

Tags
stl


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
The method contains(String) is undefined for the type String sampada Programming 1 06-30-2007 10:02 AM
how to convert an integer type to string? nesta Programming 1 04-11-2007 07:15 PM
Segmentation fault cause by String type nelnel Programming 2 08-23-2005 09:46 PM
list<type> how can I make type be a pointer? exodist Programming 2 06-06-2005 08:40 AM
Problem with copying string alltime Programming 5 04-13-2005 09:05 PM


All times are GMT -5. The time now is 08:08 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration