当前位置:网站首页>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【总结】
前提就是指针变量 作为 结构体的成员
浅拷贝:两个结构体变量 中的 指针成员 指向 同一块堆区空间。
深拷贝:两个结构体变量 中的 指针成员 指向 各自的堆区空间。
边栏推荐
- May brush question 01 - array
- C#/. Net phase VI 01C Foundation_ 01: running environment, process of creating new C program, strict case sensitivity, meaning of class library
- MapReduce instance (IV): natural sorting
- 五月刷题26——并查集
- May brush question 27 - figure
- Hero League rotation chart manual rotation
- In order to get an offer, "I believe that hard work will make great achievements
- 112 pages of mathematical knowledge sorting! Machine learning - a review of fundamentals of mathematics pptx
- If a university wants to choose to study automation, what books can it read in advance?
- Why data Tiering
猜你喜欢

Elk project monitoring platform deployment + deployment of detailed use (II)

【深度学习】语义分割:论文阅读(NeurIPS 2021)MaskFormer: per-pixel classification is not all you need

MapReduce instance (x): chainmapreduce
[Yu Yue education] Wuhan University of science and technology securities investment reference

Combined search /dfs solution - leetcode daily question - number of 1020 enclaves

Mapreduce实例(八):Map端join

Counter attack of noodles: redis asked 52 questions in a series, with detailed pictures and pictures. Now the interview is stable

A wave of open source notebooks is coming

Mapreduce实例(十):ChainMapReduce

Detailed explanation of cookies and sessions
随机推荐
六月刷题02——字符串
Control the operation of the test module through the panel in canoe (Advanced)
What you have to know about network IO model
Vs All comments and uncomments
五月集训总结——来自阿光
【深度学习】语义分割:论文阅读:(CVPR 2022) MPViT(CNN+Transformer):用于密集预测的多路径视觉Transformer
单片机如何从上电复位执行到main函数?
Redis distributed lock implementation redison 15 questions
Several silly built-in functions about relative path / absolute path operation in CAPL script
五月刷题27——图
Vh6501 Learning Series
Defensive C language programming in embedded development
小白带你重游Spark生态圈!
Selection of software load balancing and hardware load balancing
[Yu Yue education] reference materials of complex variable function and integral transformation of Shenyang University of Technology
May brush question 27 - figure
[deep learning] semantic segmentation - source code summary
[untitled]
CAPL 脚本打印函数 write ,writeEx ,writeLineEx ,writeToLog ,writeToLogEx ,writeDbgLevel 你真的分的清楚什么情况下用哪个吗?
面试突击62:group by 有哪些注意事项?