当前位置:网站首页>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 .
边栏推荐
- flask运维脚本(长时间运行)
- Some thoughts on the study of 51 single chip microcomputer
- What you have to know about network IO model
- 面试突击62:group by 有哪些注意事项?
- CANoe的数据回放(Replay Block),还是要结合CAPL脚本才能说的明白
- Yarn organizational structure
- C#/. Net phase VI 01C Foundation_ 01: running environment, process of creating new C program, strict case sensitivity, meaning of class library
- Random notes
- 美疾控中心:美国李斯特菌疫情暴发与冰激凌产品有关
- Some thoughts on the study of 51 single chip microcomputer
猜你喜欢
C杂讲 浅拷贝 与 深拷贝
PR 2021 quick start tutorial, first understanding the Premiere Pro working interface
Several silly built-in functions about relative path / absolute path operation in CAPL script
面试突击62:group by 有哪些注意事项?
51单片机进修的一些感悟
Elk project monitoring platform deployment + deployment of detailed use (II)
Installation de la pagode et déploiement du projet flask
I2C summary (single host and multi host)
Cap theory
What you have to know about network IO model
随机推荐
Keep these four requirements in mind when learning single chip microcomputer with zero foundation and avoid detours
How does the single chip microcomputer execute the main function from power on reset?
为什么大学单片机课上51+汇编,为什么不直接来STM32
Canoe cannot automatically identify serial port number? Then encapsulate a DLL so that it must work
大学C语言入门到底怎么学才可以走捷径
Safety notes
Listen to my advice and learn according to this embedded curriculum content and curriculum system
Solve the problem of too many small files
Hugo blog graphical writing tool -- QT practice
Teach you how to write the first MCU program hand in hand
May brush question 26 - concurrent search
Retention policy of RMAN backup
Control the operation of the test module through the panel in canoe (Advanced)
Canoe CAPL file operation directory collection
018. Valid palindromes
Cmooc Internet + education
A wave of open source notebooks is coming
零基础学习单片机切记这四点要求,少走弯路
Programmation défensive en langage C dans le développement intégré
Function description of shell command parser