Shallow copy and deep copy in C

前端 未结 2 1388
暖寄归人
暖寄归人 2020-12-05 05:49

I tried googling this but only objected oriented languages pop up as results.

From my understanding a shallow copy is copying certain members of a struct.

so

2条回答
  •  北荒
    北荒 (楼主)
    2020-12-05 06:30

    The copy constructor is used to initialize the new object with the previously created object of the same class. By default compiler wrote a shallow copy. Shallow copy works fine when dynamic memory allocation is not involved because when dynamic memory allocation is involved then both objects will points towards the same memory location in a heap, Therefore to remove this problem we wrote deep copy so both objects have their own copy of attributes in a memory. In order to read the details with complete examples and explanations you could see the portion of this article about difference between Shallow and Deep copy constructors.

提交回复
热议问题