当前位置:网站首页>C杂讲 浅拷贝 与 深拷贝
C杂讲 浅拷贝 与 深拷贝
2022-07-06 09:04:00 【Bright-SKY】
目录
知识点1【知识点的引入】
1、当结构体中存在指针成员变量时,进行结构体变量拷贝操作时,存在浅拷贝与深拷贝差别问题。
即:浅拷贝时,拷贝后两个结构体变量中指针成员变量指向同一个地址
深拷贝时,拷贝后两个结构体变量中指针成员变量指向不同地址
typedef struct
{
int num;
char *name;//指针变量作为 结构体的成员
}DATA;
void test01()
{
DATA data={100,"hehehehaha"};
printf("%d\n",sizeof(DATA));//8字节
printf("num = %d\n",data.num);
//指针变量作为结构体的成员 保存的是空间的地址
printf("name = %s\n",data.name);
}
2、指针变量 作为结构体的成员 操作前 必须有合法的空间
void test02()
{
DATA data;
printf("%d\n",sizeof(DATA));
printf("num = %d\n",data.num);
//指针变量 作为结构体的成员 操作前 必须有合法的空间
//data.name = "hehe";
//给name 事先申请 一块 堆区空间
data.name = (char *)calloc(1,10);
strcpy(data.name,"hahaha");
printf("name = %s\n",data.name);
//如果name指向堆区空间 一定要记得释放
if(data.name != NULL)
{
free(data.name);
data.name = NULL;
}
}
原理图分析:
3、指针变量 作为结构体的成员 结构体变量间的赋值操作 容易导致“浅拷贝”发生
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);
//指针变量 作为结构体的成员 结构体变量间的赋值操作 容易导致“浅拷贝”发生
data2 = data1;//“浅拷贝”
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;
}
}
运行结果 出现段错误
知识点2【深拷贝】
#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;
//为结构体变量 申请 独立的空间
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;
}
运行结果:
知识点3【总结】
前提就是指针变量 作为 结构体的成员
浅拷贝:两个结构体变量 中的 指针成员 指向 同一块堆区空间。
深拷贝:两个结构体变量 中的 指针成员 指向 各自的堆区空间。
边栏推荐
- Popularization of security knowledge - twelve moves to protect mobile phones from network attacks
- 数据建模有哪些模型
- Leetcode:608 tree node
- Download address of canoe, download and activation of can demo 16, and appendix of all canoe software versions
- Release of the sample chapter of "uncover the secrets of asp.net core 6 framework" [200 pages /5 chapters]
- 018. Valid palindromes
- 面试突击62:group by 有哪些注意事项?
- Research and implementation of hospital management inpatient system based on b/s (attached: source code paper SQL file)
- [deep learning] semantic segmentation - source code summary
- Why data Tiering
猜你喜欢
MapReduce instance (x): chainmapreduce
【深度学习】语义分割:论文阅读:(2021-12)Mask2Former
MapReduce instance (VI): inverted index
MapReduce instance (VIII): Map end join
小白带你重游Spark生态圈!
max-flow min-cut
Download address of canoe, download and activation of can demo 16, and appendix of all canoe software versions
Oom happened. Do you know the reason and how to solve it?
Cap theory
CAPL脚本中关于相对路径/绝对路径操作的几个傻傻分不清的内置函数
随机推荐
MapReduce instance (IX): reduce end join
[untitled]
CAPL 脚本打印函数 write ,writeEx ,writeLineEx ,writeToLog ,writeToLogEx ,writeDbgLevel 你真的分的清楚什么情况下用哪个吗?
大学想要选择学习自动化专业,可以看什么书去提前了解?
数据建模有哪些模型
Summary of May training - from a Guang
[Yu Yue education] reference materials of complex variable function and integral transformation of Shenyang University of Technology
Une grande vague d'attaques à la source ouverte
Design and implementation of online snack sales system based on b/s (attached: source code paper SQL file)
How does the single chip microcomputer execute the main function from power on reset?
May brush question 02 - string
Why is 51+ assembly in college SCM class? Why not come directly to STM32
max-flow min-cut
面试突击62:group by 有哪些注意事项?
[CV] target detection: derivation of common terms and map evaluation indicators
【深度學習】語義分割-源代碼匯總
A wave of open source notebooks is coming
CANoe CAPL文件操作目录合集
Can I learn PLC at the age of 33
Workflow - activiti7 environment setup