#### Archived

This topic is now archived and is closed to further replies.

# How shallow copy works?

This topic is 5666 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

SomeStruct a, b; .. a = b; Given the condition that the struct don''t have assignment operator, and NO pointers BUT MFC''s CString and some basic types as variables, how the assignment work? 1) var_a = var_b for every member variables 2) memcpy(a, b, sizeof b); // or similar .. can anyone enlighten me here? I''m not having bugs actually, I just got bored with my work here.

##### Share on other sites
aren''t #1 and #2 the same, really? #1 is how it''s "done" anyway, AFAIK.

"I have a cursing flasher."

##### Share on other sites
So long as each of your struct''s members is a primative data type or implements it''s own copy constructor, I do believe that you should be fine doing a ''shallow copy'' on the top level struct.

HTH

##### Share on other sites
The default generated assignment op will do 1). In the case of POD only types as you have here, 1) and 2) are equivalent.

##### Share on other sites
Ok. Now let's say we have std::string (which have those Copy-on-Write feature ) as members, 1) and 2) do have difference. I once told they do bit-copy for default assignment, which means something like memcpy(). If that the case, I don't think struct with std::string or CString will work correctly. Since std::string and CString do have pointers within them, memcpy()-like assignment would screwed up everything.
struct SomeStruct{  std::string name;  int age;};SomeStruct a,b,c,d;...b = a;                   // are they using memcpy() for this?memcpy(d, c, sizeof c);  // do this have problem?

Anyone??

[edited by - DerekSaw on August 14, 2002 10:14:53 PM]

##### Share on other sites
quote:
Original post by DerekSaw
Ok. Now let''s say we have std::string (which have those Copy-on-Write feature )

This isn''t guaranteed. In fact, I''ve never found an std::string which did support copy-on-write, because it''s next to impossible to implement thread safety using such a model.

quote:
I once told they do bit-copy for default assignment, which means something like memcpy().

You were told wrong. The default assignment operator calls the assignment operator on each of its members. This will usually be the equivalent of memcpy for arrays of normal variables or simple structs (''PODs'' as mentioned above), or the assignment operator for classes or structs that have them defined. std::string has an assignment operator so it will be used.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files | My stuff ]

##### Share on other sites
That''s great! Thanx.

.. and about the copy-on-write, is it true that std::string doesn''t have it?... I agreed it is not-so tread safety for COW and ref-counted obj since they have ''similar'' (but different) ways to implement.

p/s: Sorry for putting this into the wrong Forum. I guess this belongs to General Programming. My bad... I just noticed that this is under Game Programming.

##### Share on other sites
std::string might have copy-on-write. The standard was written in such a way that it can, as far as I know. However, I remember reading that it was nearly impossible to provide a string that can provide the full efficiency of copy-on-write with the ability to be 100% threadsafe. Therefore most vendors err on the side of thread safety as they don't want the standard library to be unusable under multithreaded conditions. Look at From Mechanism to Method: Distinctly Qualified for some opinions on this.

Just a quick addition: I said that it will use the assignment operator above. However, I don't know what it will do if you didn't define the assignment operator but did define a copy constructor. Perhaps it would use that instead - I am a bit fuzzy on this since I always define both for my classes. The best way for you to find out is to test it with a cout in each function. That's a good way for you to prove if I was right or wrong anyway. It's always satisfying to prove something to yourself.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files | My stuff ]

[edited by - Kylotan on August 16, 2002 1:16:57 AM]

##### Share on other sites
I don''t think that (2) would work. Doesn''t CString and std::string contain pointer to the character buffer? After memcpy() there would be 2 different string objects holding a pointer to the same buffer. And when one of them gets destroyed, it frees the buffer, making other strings pointer invalid. Also, when one of the objects modifies the buffer it has undefined results on the other string objects behavior..

##### Share on other sites
quote:
Original post by Kylotan
The best way for you to find out is to test it with a cout in each function. That''s a good way for you to prove if I was right or wrong anyway. It''s always satisfying to prove something to yourself.

I hope ALL the compilers implement the same behavior.