Re: deep copy or shallow copy?
On 7/25/2014 6:53 PM, Lynn McGuire wrote:
On 6/24/2014 12:53 PM, mbalover2014@gmail.com wrote:
Hi all,
lets assume that I have a structure defined as
struct A
{
float arr[100];
}
A a1;
A a2;
// Initialize a1, a2.
memcpy(&a1,&a2,sizeof(A));
is it a deep copy or still shallow copy?
Also how about a2 = a1; is it a deep copy or shallow copy from a1 to a2?
Thank you.
First, do not use structs in C++ code. Sadly, we
have a lot of old C code that was promoted to C++
by changing the .c to a .cpp so we have structs
all over the place.
What's wrong with using 'struct'?
That said, the nice thing about the STL is that it
allows you to go shallow or deep. You can use:
std::vector <myClass> anObj;
or
std::vector <myClass *> anObj;
We frequently use the pointer variant in order to
just make a temporary list of objects subject to
some criteria. Passing around pointers is a lot
faster and the corresponding new and delete's for
these objects is a very controlled situation. We
have very little memory leakage in our app of
700K lines of C++ code.
So, for the sake of some miniscule (likely guessed, not measured)
performance gain you risk "very little memory leakage"? What's the big
idea, a little leakage is OK, but a little time spent copying object is
a no-no?
V
--
I do not respond to top-posted replies, please don't ask