资讯专栏INFORMATION COLUMN

【动态内存管理】动态内存分配、常见错误、经典笔试题、柔性数组

Songlcy / 2976人阅读

摘要:如果开辟失败,则返回一个指针,因此的返回值一定要做检查。函数用来释放动态开辟的内存。


一、动态内存分配

1、为什么存在动态内存分配

  1. 空间开辟大小是固定的
  2. 数组在声明的时候,必须指定数组的长度,它所需要的内存在编译时分配
  1. 堆区
  2. malloc calloc realloc free

二、malloc

在堆区上申请size_t大小的空间 返回这块空间的起始位置

void* malloc(size_t t);

1、malloc、free

这个函数向内存申请一块连续可用的空间,并返回指向这块空间的指针。

  1. 如果开辟成功,则返回一个指向开辟好空间的指针。
  2. 如果开辟失败,则返回一个NULL指针,因此malloc的返回值一定要做检查。
  3. 返回值的类型是 void* ,所以malloc函数并不知道开辟空间的类型,具体在使用的时候使用者自己来决定。
  4. 如果参数 size 为0,malloc的行为是标准是未定义的,取决于编译器。

free函数用来释放动态开辟的内存。

  1. 如果参数 ptr 指向的空间不是动态开辟的,那free函数的行为是未定义的。
  2. 如果参数 ptr 是NULL指针,则函数什么事都不做。
#include #include int main(){	// 1. 开辟空间	int* p = (int*)malloc(40); // 申请40大小空间 把起始地址强转为int* 赋给p	if (p == NULL)	{		return -1;	}	// 开辟成功了 可以使用	int i = 0;	for (i = 0; i < 10; i++)	{		*(p + i) = i;	}	// 2. 释放空间	free(p);	p = NULL; // 	return 0;}

2、calloc

void* calloc (size_t num, size_t size);

2.1、与malloc 的区别

malloc函数只负责在堆区申请空间,并且返回起始地址,不初始化空间
calloc函数在堆区上申请空间,并且在返回起始地址之前把申请的每个字节初始化为0

#include #include #include int main(){	int* p = (int*)calloc(10, sizeof(int));	if (p == NULL)	{		printf("%s/n", strerror(errno));		return -1;	}	// 申请成功	int i = 0;	for (i = 0; i < 10; i++)	{		printf("%d ", *(p + i));	}	// 释放空间	free(p);	p = NULL;	return 0;}

3、realloc

让动态内存管理更加灵活

void* realloc (void* ptr, size_t size);

ptr 是要调整的内存地址
size 调整之后新大小
返回值为调整之后的内存起始位置。
这个函数调整原内存空间大小的基础上,还会将原来内存中的数据移动到 新 的空间

两种情况:

  1. 后面空间大小够用,直接在后面开辟
  2. 空间不够,在堆空间上另找一个合适大小的连续空间来使用,这样函数返回的是一个新的内存地址
#include #include #include int main(){	int* p = (int*)calloc(10, sizeof(int));	if (p == NULL)	{		printf("%s/n", strerror(errno));		return -1;	}	int i = 0;	for (i = 0; i < 10; i++)	{		*(p + i) = i;	}	// 空间不够大,增加空间至20int	int* ptr = (int*)realloc(p, 20 * sizeof(int));	if (ptr != NULL)	{		p = ptr;	}	else	{		return -1;	}	// 增加成功,使用	for (i = 10; i < 20; i++)	{		*(p + i) = i;	}	for (i = 0; i < 20; i++)	{		printf("%d ", *(p + i));	}	free(p);	p = NULL;	return 0;}

4、常见错误

4.1、 对malloc返回值判断

int* p = (int*)malloc(20);*p = 0; // 有风险
#include #include int main(){	int* p = (int*)malloc(20);	if (p == NULL)	{		return -1;	}	*p = 0;	return 0;}

4.2、对动态内存空间的越界访问

#include #include int main(){	int* p = (int*)malloc(200);	if (p == NULL)	{		return -1;	}	int i = 0;	for (i = 0; i < 80; i++)	{		*(p + i) = 1;	}		for (i = 0; i < 80; i++)	{		printf("%d ", *(p + i));	}	free(p);	p = NULL;	return 0;}

4.3、释放非动态内存空间

int main(){	int a = 10;	int* p = &a;	free(p); // err	p = NULL;	return 0;}

4.4、使用free释放一块动态开辟内存的一部分

改变了p 不再指向起始位置 此时释放的不在 起始位置

#include #include int main(){	int* p = (int*)malloc(10 * sizeof(int));	if (p == NULL)	{		return -1;	}	int i = 0;	for (i = 0; i < 10; i++)	{		*p++ = 1;	}	free(p);	p = NULL;	return 0;}

4.5、对同一块动态内存多次释放

int main(){	int* p = (int*)malloc(40);	if (p == NULL)	{		return -1;	}	free(p);	free(p); // err}int main(){	int* p = (int*)malloc(40);	if (p == NULL)	{		return -1;	}	free(p);	p = NULL;	free(p); // ok}

4.6、动态开辟内存忘记释放(内存泄漏)

在堆区上申请空间,有2种回收方式,

  1. free
  2. 程序退出时,申请的空间回收
int main(){	int* p = (int*)malloc(40);	if (p == NULL)	{		return -1;	}	// 没有释放	return 0;}


三、经典笔试题

题目一:

#include #include void GetMemory(char* p){	p = (char*)malloc(100);} void Test(void){	char* str = NULL;	GetMemory(str); 	strcpy(str, "hello world");	printf(str);} int main(){	Test();	return 0;}

程序会崩溃

  1. str传给p的时候,是值传递,p是str的临时拷贝,所以当malloc开辟的空间起始地址放在p中时,不会影响str,str仍是NULL
  2. 当str是NULL,strcpy想把hello world拷贝到str指向的空间时,程序就会崩溃,因为NULL指针指向的空间时不能直接访问的
  3. 存在内存泄漏,出函数销毁,无法回收空间

修改:
版本1:

#include #include #include void GetMemory(char** p){	*p = (char*)malloc(100);} void Test(void){	char* str = NULL;	GetMemory(&str); // char** 	strcpy(str, "hello world");	printf(str);	// 释放	free(str);	str = NULL;} int main(){	Test();	return 0;}

版本2:

#include #include #include char* GetMemory(char* p){	p = (char*)malloc(100); 	return p;} void Test(void){	char* str = NULL; 	str = GetMemory(str);	strcpy(str, "hello world"); 	printf(str);		free(str);	str = NULL;} int main(){	Test();	return 0;}

题目二:

#include char* GetMemory(void){	char p[] = "hello world";	return p;} void Test(void){	char* str = NULL;	str = GetMemory();	printf(str);} int main(){	Test();	return 0;}

【返回栈空间地址问题】

#include int* test(){	int n = 10;	return &n;} int main(){	int* p = test();	printf("%d/n", *p);	// 如果没有被覆盖,有可能输出10		return 0;}

题目三:

#include void GetMemory(char** p, int num){	*p = (char*)malloc(num);}void Test(void){	char* str = NULL;	GetMemory(&str, 100);	strcpy(str, "hello");	printf(str);}int main(){	Test();	return 0;}

通过*p 用malloc给str在堆上开辟空间,
问题:

内存泄漏,没有free
free(str);
str = NULL;

改正:

#include #include void GetMemory(char** p, int num){	*p = (char*)malloc(num);}void Test(void){	char* str = NULL;	GetMemory(&str, 100);	strcpy(str, "hello");	printf(str);	free(str);	str = NULL;}int main(){	Test();	return 0;}

题目四:

#include #include void Test(void){	char* str = (char*)malloc(100);	strcpy(str, "hello");	free(str);	if (str != NULL)	{		strcpy(str, "world");		printf(str);	}}int main(){	Test();	return 0;}

改正:

#include #include void Test(void){	char* str = (char*)malloc(100);	strcpy(str, "hello");	free(str);	str = NULL;	if (str != NULL)	{		strcpy(str, "world");		printf(str);	}}int main(){	Test();	return 0;}


四、C/C++程序的内存开辟



五、柔性数组

1、柔性数组成员

C99 中,结构中的最后一个元素允许是未知大小的数组,这就叫做『柔性数组』成员。

// 数组大小不确定,可大可小typedef struct st_type{	int i;	int a[0];//柔性数组成员}type_a;// 编译器报错无法编译可改成:typedef struct st_type{	int i;	int a[];//柔性数组成员}type_a;

2、柔性数组的特点:

1. 结构中的柔性数组成员前面必须至少一个其他成员	如 int a[] 前有 int i2. sizeof 返回的这种结构大小不包括柔性数组的内存
#include typedef struct <           
               
                                           
                       
                 

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/121274.html

相关文章

  • C语言进阶:动态内存管理

    摘要:释放不完全导致内存泄漏。既然把柔性数组放在动态内存管理一章,可见二者有必然的联系。包含柔性数组的结构用进行动态内存分配,且分配的内存应大于结构大小,以满足柔性数组的预期。使用含柔性数组的结构体,需配合以等动态内存分配函数。 ...

    shinezejian 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<