Copying the contents of a struct from a pointer
-
Hi, here's my problem: I have a pointer to a struct (in C++, using VC++ .NET 2005 with MFC). The struct has been allocated from heap memory and contains many fields that are also allocated from the heap (character arrays, etc). How can I copy this straight to another struct? These are the two things I've tried, neither have worked: This is where oldData is a pointer to a MyStruct that has been allocated from the heap:
struct MyStruct *newData = new MyStruct; memcpy(newData, oldData, sizeof(struct MyStruct)); // Do stuff, then copy the data back memcpy(oldData, newData, sizeof(struct MyStruct)); delete newData;
And:struct MyStruct newData; newData = *oldData; // Do stuff, then copy the data back oldData = &newData;
Neither of these methods work. Is there a way to do this or do I have to copy field by field with the proper memory allocations in newData etc? Thanks!KR
-
Hi, here's my problem: I have a pointer to a struct (in C++, using VC++ .NET 2005 with MFC). The struct has been allocated from heap memory and contains many fields that are also allocated from the heap (character arrays, etc). How can I copy this straight to another struct? These are the two things I've tried, neither have worked: This is where oldData is a pointer to a MyStruct that has been allocated from the heap:
struct MyStruct *newData = new MyStruct; memcpy(newData, oldData, sizeof(struct MyStruct)); // Do stuff, then copy the data back memcpy(oldData, newData, sizeof(struct MyStruct)); delete newData;
And:struct MyStruct newData; newData = *oldData; // Do stuff, then copy the data back oldData = &newData;
Neither of these methods work. Is there a way to do this or do I have to copy field by field with the proper memory allocations in newData etc? Thanks!KR
-
Hi, here's my problem: I have a pointer to a struct (in C++, using VC++ .NET 2005 with MFC). The struct has been allocated from heap memory and contains many fields that are also allocated from the heap (character arrays, etc). How can I copy this straight to another struct? These are the two things I've tried, neither have worked: This is where oldData is a pointer to a MyStruct that has been allocated from the heap:
struct MyStruct *newData = new MyStruct; memcpy(newData, oldData, sizeof(struct MyStruct)); // Do stuff, then copy the data back memcpy(oldData, newData, sizeof(struct MyStruct)); delete newData;
And:struct MyStruct newData; newData = *oldData; // Do stuff, then copy the data back oldData = &newData;
Neither of these methods work. Is there a way to do this or do I have to copy field by field with the proper memory allocations in newData etc? Thanks!KR
KellyR wrote:
Neither of these methods work.
No surprise there, since
memcpy()
has no way of getting to that other heap data. It only knows about the data in theMyStruct
structure.KellyR wrote:
Is there a way to do this or do I have to copy field by field with the proper memory allocations in newData etc?
I would implement an assignemnt operator in the structure:
const MyStruct& operator=( const MyStruct &b )
{
...
return *this;
}"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne