当前位置:网站首页>C miscellaneous shallow copy and deep copy
C miscellaneous shallow copy and deep copy
2022-07-06 09:59:00 【Bright-SKY】
Catalog
Knowledge point 1【 Introduction of knowledge points 】
Knowledge point 2【 Deep copy 】
Knowledge point 1【 Introduction of knowledge points 】
1、 When there are pointer member variables in the structure , When copying structural variables , There is a difference between shallow copy and deep copy .
namely : In light copy , After copying, the pointer member variable in the two structure variables points to The same Address
Deep copy , After copying, the pointer member variable in the two structure variables points to Different Address
typedef struct
{
int num;
char *name;// Pointer variable as Member of a structure
}DATA;
void test01()
{
DATA data={100,"hehehehaha"};
printf("%d\n",sizeof(DATA));//8 byte
printf("num = %d\n",data.num);
// Pointer variable as a member of the structure Save the address of the space
printf("name = %s\n",data.name);
}
2、 Pointer to the variable As a member of a structure Before operation There must be legal space
void test02()
{
DATA data;
printf("%d\n",sizeof(DATA));
printf("num = %d\n",data.num);
// Pointer to the variable As a member of a structure Before operation There must be legal space
//data.name = "hehe";
// to name Apply in advance A piece of Heap space
data.name = (char *)calloc(1,10);
strcpy(data.name,"hahaha");
printf("name = %s\n",data.name);
// If name Point to heap space Be sure to release
if(data.name != NULL)
{
free(data.name);
data.name = NULL;
}
}
Schematic analysis :
3、 Pointer to the variable As a member of a structure Assignment between structural variables Easily lead to “ Shallow copy ” happen
void test03()
{
DATA data1;
DATA data2;
data1.num = 100;
data1.name = (char *)calloc(1,10);
strcpy(data1.name,"my data");
printf("data1:num = %d, name = %s\n",data1.num, data1.name);
// Pointer to the variable As a member of a structure Assignment between structural variables Easily lead to “ Shallow copy ” happen
data2 = data1;//“ Shallow copy ”
printf("data2: num = %d, name = %s\n",data2.num, data2.name);
if(data1.name != NULL)
{
free(data1.name);
data1.name = NULL;
}
if(data2.name != NULL)
{
free(data2.name);
data2.name = NULL;
}
}
Running results There was a paragraph error
Knowledge point 2【 Deep copy 】
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct
{
int num;
char *name;
}DATA;
void test01()
{
DATA data1;
DATA data2;
data1.num = 100;
data1.name = (char *)calloc(1,12);
strcpy(data1.name, "my data");
data2.num = data1.num;
// Is the structure variable apply Independent space
data2.name = (char *)calloc(1,12);
strcpy(data2.name, data1.name);
printf("data1:num = %d, name=%s\n", data1.num, data1.name);
printf("data2:num = %d, name=%s\n", data2.num, data2.name);
if(data1.name != NULL)
{
free(data1.name);
data1.name = NULL;
}
if(data2.name != NULL)
{
free(data2.name);
data2.name = NULL;
}
}
int main(int argc,char *argv[])
{
test01();
return 0;
}
Running results :
Knowledge point 3【 summary 】
The premise is pointer variables As Member of a structure
Shallow copy : Two structural variables Medium Pointer members Point to Same stack space .
Deep copy : Two structural variables Medium Pointer members Point to Respective heap space .
边栏推荐
- Canoe cannot automatically identify serial port number? Then encapsulate a DLL so that it must work
- 51单片机进修的一些感悟
- Several silly built-in functions about relative path / absolute path operation in CAPL script
- Can I learn PLC at the age of 33
- 一大波开源小抄来袭
- Release of the sample chapter of "uncover the secrets of asp.net core 6 framework" [200 pages /5 chapters]
- Listen to my advice and learn according to this embedded curriculum content and curriculum system
- Pointer learning
- 简单解决phpjm加密问题 免费phpjm解密工具
- Teach you how to write the first MCU program hand in hand
猜你喜欢
面试突击62:group by 有哪些注意事项?
Redis distributed lock implementation redison 15 questions
History of object recognition
PR 2021 quick start tutorial, first understanding the Premiere Pro working interface
Une grande vague d'attaques à la source ouverte
16 医疗挂号系统_【预约下单】
13 医疗挂号系统_【 微信登录】
Solve the problem of too many small files
手把手教您怎么编写第一个单片机程序
CANoe下载地址以及CAN Demo 16的下载与激活,并附录所有CANoe软件版本
随机推荐
Contest3145 - the 37th game of 2021 freshman individual training match_ C: Tour guide
Bugku web guide
The 32-year-old fitness coach turned to a programmer and got an offer of 760000 a year. The experience of this older coder caused heated discussion
手把手教您怎么编写第一个单片机程序
CAPL脚本中关于相对路径/绝对路径操作的几个傻傻分不清的内置函数
Why can't TN-C use 2p circuit breaker?
Why data Tiering
Nc29 search in two-dimensional array
14 医疗挂号系统_【阿里云OSS、用户认证与就诊人】
13 医疗挂号系统_【 微信登录】
Competition vscode Configuration Guide
MapReduce instance (IX): reduce end join
15 医疗挂号系统_【预约挂号】
Inject common SQL statement collation
CAPL 脚本对.ini 配置文件的高阶操作
Function description of shell command parser
The programming ranking list came out in February. Is the result as you expected?
CAPL 脚本打印函数 write ,writeEx ,writeLineEx ,writeToLog ,writeToLogEx ,writeDbgLevel 你真的分的清楚什么情况下用哪个吗?
[CV] target detection: derivation of common terms and map evaluation indicators
Solve the problem of too many small files