Deep copy, copy the contents of the memory, the old structure changes, the new structure will also change.
Shallow copy, direct address replication, shared memory, new and old structure complementary effects.
1 #define_crt_secure_no_warnings2 3#include <stdio.h>4#include <stdlib.h>5#include <string.h>6 7 struct string8 {9 Char*p;Ten intlength; One }; A - main1 () - { the struct stringstr1; -Str1.length =Ten; -STR1.P = (Char*)malloc(sizeof(Char) *Ten); -strcpy (STR1.P,"Hello"); + -printf"str1.p=%s\n", STR1.P); + A struct stringstr2;//shallow copy, shared memory atStr2.length =str1.length; -STR2.P =STR1.P; -* (STR1.P) ='k'; - -printf"str1.p=%s\n", STR1.P); -printf"str2.p=%s\n", STR2.P); in -System"Pause"); to } + - Main () the { * struct stringstr1; $Str1.length =Ten;Panax NotoginsengSTR1.P = (Char*)malloc(sizeof(Char) *Ten); -strcpy (STR1.P,"Hello"); the +printf"str1.p=%s\n", STR1.P); A the struct stringstr2;//Deep Copy +Str2.length =str1.length; -STR2.P = (Char*)malloc(sizeof(Char) *Ten); $ strcpy (STR2.P, STR1.P); $ -printf"str1.p=%s\n", STR1.P); -printf"str2.p=%s\n", STR2.P); the -System"Pause");Wuyi}
Deep copy _ Shallow copy