S
spoc
I have been using memcpy to copy one class to another of the same type.
There are reasons why I had to do this bug am getting some odd crashes and
maybe I'm doing something dodgy copying classes like this? The classes
contain no dynamicaly allocated data, just standard types and arrays. Here
is an example of what I'm doing, I know in isolation it may seem odd. I am
presuming this will copy all data ok but not really sure of the workings and
implications of doing it this way:
o* c1 = new o();
o* c2 = new o();
// various inits on both c1 and c2...
// copy c2 over c1 and delete c2
memcpy(c1, c2, sizeof(*c1));
delete c2;
// continue working with c1 (which is a copy of the deleted c2)
Is this ok?
Cheers, spoc
There are reasons why I had to do this bug am getting some odd crashes and
maybe I'm doing something dodgy copying classes like this? The classes
contain no dynamicaly allocated data, just standard types and arrays. Here
is an example of what I'm doing, I know in isolation it may seem odd. I am
presuming this will copy all data ok but not really sure of the workings and
implications of doing it this way:
o* c1 = new o();
o* c2 = new o();
// various inits on both c1 and c2...
// copy c2 over c1 and delete c2
memcpy(c1, c2, sizeof(*c1));
delete c2;
// continue working with c1 (which is a copy of the deleted c2)
Is this ok?
Cheers, spoc