Possible Duplicate:
Copying one structure to another
I have a struct defined as
struct CKTcircuit
{
more than a 100 pointers and objects of different types of structure.
this structure has around 500 lines just declaring variables & pointers.
}
Now I have a object of this type instantiated.
And I have to create a copy this same type.
Any solutions how do I do this?
P.S.: I need to do this because I need to launch another thread doing computations and modifying the CKTcircuit object.
1) The hard way:
For a deep copy you’ll need to traverse the entire structure and recreate it one node at a time. But make sure you use the pointers from the copy and not from the original data structure.
here’s a simplified pseudo code example.
2) Short cuts
A) Contiguous memory
If you could guarantee that your memory was contiguously allocated (malloc once and then hand out the memory on your own), then you can just memcpy the whole block and then search for addresses in that address space and just modify them. But, that’s not very robust.
B) Array of struct
Use a preallocated array of your structures and indices instead of pointers. But that won’t work for a heterogenous data structure. This is basically a rewrite of your program.