Reputation: 49
Suppose I have a
struct A{
char *name;
unsigned long *trunks;
bool value;
const struct smap *smap;
...
...
}
This struct has all types of data structures and I do not have direct exposure to the struct apart from struct A A*, which is a pointer to it.
Upvotes: 0
Views: 2833
Reputation: 12262
You have to copy every element in the struct and all referenced objects to newly allocated structures the same way (recursively).
If the struct has only few pointers, you might use memcpy to copy all elements as-is first and then copy all referenced (through pointers) objects in a second pass. If there are many pointers, it might be more efficient to copy each field by hand.
Referenced objects must be treated identical (by recursion, iteration would be pretty nasty). However, for this, you need to know the structure of these types. Alternatively, there might be copy functions for all these objects in their implemenation file, thus keeping them opaque. If neither the structure, nor a copy function is available, you are somewhat lost, as there is no way to detect the pointers without that.
A problem will arise if there are circular references. Then things get even more complicated.
Upvotes: 1