Understanding Deep Copy and Shallow Copy in C

Understanding Deep Copy and Shallow Copy in C

Deep Copy

A deep copy in programming creates a new object that is an exact replica of another object, duplicating all its data. This new object is stored in a separate memory location, ensuring that changes to the original do not affect the copy, and vice versa.

How to Achieve Deep Copy in C

In C, deep copying is usually done by manually copying each element of the object. This can be achieved using functions like memcpy() or by iterating through elements and copying them one by one. For example, if you’re dealing with an array, you would loop through each element and copy it to the new array.

c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
char* name;
int age;
} Person;

Person* deepCopy(Person* original) {
Person* copy = (Person*)malloc(sizeof(Person));
copy->age = original->age;
copy->name = (char*)malloc(strlen(original->name) + 1);
strcpy(copy->name, original->name);
return copy;
}

Advantages of Deep Copy

  • Independence: Changes to the copied object do not affect the original.
  • Safety: Ideal for working with mutable objects, ensuring data integrity.

Disadvantages of Deep Copy

  • Memory Intensive: Requires additional memory to store the duplicate data.
  • Time Consuming: Can be slow for large or complex objects.

Shallow Copy

A shallow copy creates a new object that references the same memory location as the original object. This means both objects share the same data, so changes to one affect the other.

How to Achieve Shallow Copy in C

In C, a shallow copy is often achieved by simply assigning the address of one object to another.

c

Person* shallowCopy(Person* original) {
return original;
}

Advantages of Shallow Copy

  • Efficiency: Faster and less memory-intensive than deep copying.
  • Useful for Shared Data: Multiple objects can access and modify the same data without duplication.

Disadvantages of Shallow Copy

  • Shared Modifications: Changes to one object affect all references.
  • Dangling Pointers: If the original object is deallocated, the copied object will point to invalid memory.

When to Use Deep Copy

  • Independent Data Manipulation: When you need to modify the copied object without affecting the original.
  • Passing/Returning Objects: Ensures the copied object remains valid after the original is out of scope.
  • Undo/Redo Functionality: Useful in applications where previous states need to be preserved.

When to Use Shallow Copy

  • Memory Efficiency: When you need multiple references to the same data, especially in a multi-threaded environment.
  • Temporary Copies: For quick, non-permanent operations that do not require independent data.

Practical Example in C

Let’s look at a practical example where deep copy and shallow copy can be applied:

c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
char* name;
int age;
} Person;

Person* createPerson(const char* name, int age) {
Person* newPerson = (Person*)malloc(sizeof(Person));
newPerson->name = (char*)malloc(strlen(name) + 1);
strcpy(newPerson->name, name);
newPerson->age = age;
return newPerson;
}

void printPerson(Person* person) {
printf("Name: %s, Age: %d\n", person->name, person->age);
}

int main() {
Person* original = createPerson("Alice", 30);

// Deep Copy
Person* deepCopyPerson = deepCopy(original);
strcpy(deepCopyPerson->name, "Bob"); // Modifying deep copy

// Shallow Copy
Person* shallowCopyPerson = shallowCopy(original);
strcpy(shallowCopyPerson->name, "Charlie"); // Modifying shallow copy

// Output
printPerson(original); // Name: Charlie, Age: 30
printPerson(deepCopyPerson); // Name: Bob, Age: 30

// Cleanup
free(original->name);
free(original);
free(deepCopyPerson->name);
free(deepCopyPerson);
// Shallow copy does not require cleanup as it shares memory with original

return 0;
}

Conclusion

Understanding the difference between deep copy and shallow copy is crucial for efficient and error-free programming in C. Deep copy is essential when you need completely independent copies of data, while shallow copy is useful for shared, quick-access scenarios. For coding classes in Ranchi, mastering these concepts will greatly enhance your programming skills and ensure robust software development practices.

See also  Counting Vowels in a String using C
Scroll to Top