memcpy should be the way to go. Donβt worry about function calls. More often than not, no. memcpy usually a built-in compiler, which means that the compiler needs to install the most efficient code possible for it, and it needs to know where it can optimize memcpies.
Do not point to incompatible pointers, and then look for them. This is the path to undefined behavior.
If you accept expression expressions and gcc ##__VA_ARGS__ , you can have an MC_base_method(BaseType,BaseMethod,Derived_ptr,...) macro MC_base_method(BaseType,BaseMethod,Derived_ptr,...) that correctly calls BaseMethod with Derived_ptr and ... if you can work with a copy of the structure as if it was the original (for example, did not indicate its own structural elements).
Here is an example with some additional sugar macro supporting OOP:
//Helper macros for some C++-like OOP in plain C
I consider this a task for compilers to optimize memcpies. However, if this is not the case, and your structures are huge, you are screwed. The same thing if your structures contain pointers to their own members (i.e. if you cannot work with a byte on a byte copy, as if it were the original).
source share