Deep copy structure to another

I have a struct that contains strings and pointers inside. Are there any library functions for a deep copy of struct to another. I do not want to make the field by copying the field, since the structure that I have is quite large.

Does glib have any feature that does the trick?

+4
source share
2 answers

You can use memcpy or memmove to copy the entire contents of the structure itself. However, since C has no introspection, copying objects with a pointer cannot be performed using a general purpose function.

Edited to add: as some commentators note, you can simply assign structures to other structures in C dialects that have been used for the past several decades, memcpy is no longer required.

+4
source

Not. A general purpose function does not have the ability to know the structure of your structure (i.e., Information available only at compile time). And even if this happened, how would he know what constitutes a β€œdeep copy” under any circumstances?

+6
source

Source: https://habr.com/ru/post/1398709/


All Articles