C has no built-in copy constructor, but you can get the same effect with a function that creates a new struct from an existing one. For structs that only hold plain values, assignment is enough. For structs that own heap memory, you write a deep-copy function that allocates fresh storage and copies the data.
The usual naming is type_copy or type_clone, paired with a type_free function. Together they give you the lifetime management that C++ gets from copy constructors and destructors.
A C++ copy constructor, T(const T& other), runs automatically whenever an object is copied. In C nothing runs automatically, so the convention is:
T *T_clone(const T *src) or int T_copy(T *dst, const T *src).NULL or a non-zero code on failure, leaving no partial state behind.A shallow copy duplicates pointer values, so the copy and the original share the same heap block. A deep copy duplicates the pointed-to data as well.
| Shallow copy | Deep copy | |
|---|---|---|
| How | b = a; or memcpy | Custom function with malloc |
| Pointer members | Same address in both | Separate allocations |
| Modifying the copy | Changes the original | Original unaffected |
| Freeing | Double free if both are freed | Each struct frees its own memory |
This example is a dynamic array of integers. IntArray_clone returns a new heap-allocated copy, and IntArray_destroy releases it.
memcpy is correct here because it copies plain int values into a separate buffer. The array of items is duplicated, so changing b leaves a unchanged, and each is freed exactly once.
Here is the same struct copied by assignment. The two structs share one items buffer, so a write through one is visible through the other, and the second free is a double free.
Building with gcc -fsanitize=address reports this as a double free at the second free call, which makes the bug easy to find during testing. A related rule for API design is to document ownership: either the clone is owned by the caller, or the struct stores a reference that the owner keeps alive.
= and then freeing both copies, which is a double free.malloc and calloc results. A clone function must report failure.memcpy on overlapping regions. Use memmove when source and destination can overlap.Tools such as AddressSanitizer (-fsanitize=address) and Valgrind find double frees and leaks in code like this quickly.
A function such as T_clone or T_copy that allocates and copies the data. You must call it explicitly.
When the struct contains only values and fixed-size arrays, with no pointers to memory it owns. Then a member-wise copy is already a complete copy.
Both styles are common. Returning a pointer is simpler to read, while filling a destination works well for structs that live on the stack or inside other objects.
C++ calls the copy constructor automatically and can also define move semantics and destructors. In C, every copy and every free is an explicit call you must remember.
C has no copy constructor, so you simulate one with a clone or copy function. Use assignment for plain data, deep copy for owned memory, check every allocation, and pair each clone with a matching destroy function.