Structure Copying in C: Comprehensive Analysis of Shallow and Deep Copy

Nov 23, 2025 · Programming · 10 views · 7.8

Keywords: C Programming | Structure Copying | Shallow Deep Copy

Abstract: This article provides an in-depth examination of various methods for copying structures in C programming language, focusing on the advantages and disadvantages of direct assignment, memcpy function, and manual member copying. Through detailed code examples, it explains the considerations when copying structures containing array and pointer members, particularly emphasizing the fundamental differences between shallow and deep copy and their impact on program safety. The article also discusses the effect of structure padding on copying efficiency, offering comprehensive best practices for structure copying.

Basic Methods of Structure Copying

In C programming, structure copying is a common operation that requires careful consideration. Depending on specific requirements and structure member types, developers can choose from multiple copying strategies.

Advantages of Direct Assignment

For structures containing array members, direct assignment is the most concise and efficient choice. Consider the following example:

struct UserInfo {
    char username[32];
    size_t name_length;
} user1, user2;

strcpy(user1.username, "john_doe");
user1.name_length = strlen(user1.username);

user2 = user1;  // Direct assignment copy

The advantage of this approach lies in its code simplicity and readability. The compiler automatically handles structure size calculations, eliminating the need for manual computation by developers. More importantly, the compiler can optimize the copying process to avoid unnecessary padding byte copies, thereby improving execution efficiency.

Usage Scenarios of memcpy Function

Although memcpy can be used for structure copying, special attention must be paid to its shallow copy characteristics:

struct Data {
    char *dynamic_string;
    int value;
} src, dest;

src.dynamic_string = malloc(20);
strcpy(src.dynamic_string, "dynamic content");
src.value = 42;

memcpy(&dest, &src, sizeof(dest));  // Shallow copy

In this case, both structures' dynamic_string members point to the same memory region. Modifying one will affect the other, potentially causing difficult-to-debug program errors.

Deep Copy Handling for Pointer Members

For structures containing pointers to dynamically allocated memory, deep copy must be implemented:

struct ComplexData {
    char *text;
    int *numbers;
    size_t count;
};

void deep_copy_complex_data(struct ComplexData *dest, const struct ComplexData *src) {
    *dest = *src;  // Copy non-pointer members
    
    // Deep copy string
    dest->text = malloc(strlen(src->text) + 1);
    strcpy(dest->text, src->text);
    
    // Deep copy array
    dest->numbers = malloc(src->count * sizeof(int));
    memcpy(dest->numbers, src->numbers, src->count * sizeof(int));
}

This implementation ensures that each structure instance possesses independent resource copies, avoiding ambiguous resource ownership issues.

Structure Padding and Copying Efficiency

Compilers insert padding bytes into structures for memory alignment purposes. Direct assignment allows compilers to optimize the handling of these padding bytes, whereas memcpy faithfully copies all bytes, including padding content. In performance-sensitive scenarios, this difference may become significant.

Best Practice Recommendations

Based on the above analysis, we recommend the following structure copying strategies: prioritize direct assignment for pure value types and fixed array members; implement specialized deep copy functions for complex structures containing dynamic resources; consider using memcpy only in specific performance optimization scenarios, with full understanding of its shallow copy characteristics.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.