C语言中的浅拷贝和深拷贝

浅拷贝

首先看下面这段代码:

# include<assert.h>
# include<string.h>
#include <stdlib.h>
typedef struct Node//定义了一个结构体
{
	int size;
	char *data;
}S_Node;
int main()
{
	S_Node node1;
	node1.data = (char *)malloc(sizeof(char)*100);//指针所指向的地址
	assert(node1.data != NULL);
	strcpy(node1.data, "hello world");
	node1.size = strlen(node1.data);
	S_Node node2 = node1;
	printf("%d, %s\n", node1.size, node1.data);
	printf("%d,%s\n", node2.size, node2.data);
	free(node1.data);
	//free(node2.data);error重复释放
	return 0;
 } 

运行结果为:

node2=node1;仅仅完成浅拷贝(仅会将结构体变量的size,data存储的值给node2)data指向的空间以及其存储的数据并不会拷贝,即只是拷贝了8个字节,如下图,node2.size=node1.size=11,node2.data=node1.data都保存的只是地址1000

当free(node1.data),后free(node2.data),程序会报错。原因是free(node1.data);已经将所申请的空间释放掉了,当运行free(node2.data);就会重复释放。所以结构体一般不允许直接赋值。

深拷贝

同样分析下面这段代码:

# include<stdio.h>
# include<assert.h>
# include<string.h>
#include <stdlib.h>
typedef struct Node//结构体
{
	int size;
	char *data;
}S_Node;
void CopyNode(S_Node *node3, S_Node node1)//CopyNode 函数实现结构体变量的深拷贝
{
	node3->size = node1.size;
	node3->data = (char *)malloc(node3->size + 1);//申请空间
	assert(node3->data != NULL);
	strcpy(node3->data, node1.data);
}
int main()
{
	S_Node node1;
	node1.data = (char *)malloc(sizeof(char)*100);
	assert(node1.data != NULL);
	S_Node node3;
	CopyNode(&node3, node1);
	printf("%d, %x\n", node1.size, node1.data);
	printf("%d,%x\n", node3.size, node3.data);
	free(node1.data);
	free(node3.data);//释放申请的空间
	return 0;
 } 

node块空间来保存node.data的数据,并不是浅拷贝中仅仅拷贝地址。

运行结果为:

猜你喜欢

转载自blog.csdn.net/cyy_0802/article/details/80374812