动态内存管理(C语言)下--常见错误及大厂笔试题

本文介绍了C语言动态内存管理中常见的六大错误,包括对NULL指针解引用、越界访问、非动态内存释放、部分释放、重复释放和内存泄漏,并通过四个经典笔试题进行解析。强调了动态内存分配与释放的重要性,提醒程序员注意避免这些错误,防止程序出现未定义行为和内存泄漏。

作者:旧梦拾遗186

专栏:C语言编程----小比特成长日记

相关链接:动态内存管理(C语言)详细总结

 

每日励志:

没有人能为你承受,也没有人能夺走你的力量。每个人都是一个孤独的步行者,在行走过程中逐渐变得坚强起来

前言:

上一篇文章小编带大家学习了,动态内存管理可点击相关链接动态内存管理(C语言)详细总结,今天为加深大家对动态内存管理的理解,小编带大家认识一下动态内存管理常见的错误,以及相关大厂的笔试题目。

目录

一.常见的动态内存错误

1.1对NULL指针的解引用操作

1.2 对动态开辟空间的越界访问 

1.3 对非动态开辟内存使用free释放 

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

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

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

二.经典笔试题

 1.1 题目1:

解决方法:

 2.2 题目2:

解决方法:

2.3 题目3:  

解决方法:

2.4 题目4: 

解决方法:


一.常见的动态内存错误

1.1对NULL指针的解引用操作

#include<stdio.h>
#include<stdlib.h>
void test()
{
	int* p = (int*)malloc(INT_MAX / 4);
	*p = 20;//如果p的值是NULL,就会有问题
	free(p);
}
int main()
{
	test();
	return 0;
}

应该先判断指针是否为空!! 

1.2 对动态开辟空间的越界访问 

#include<stdio.h>
#include<stdlib.h>
void test()
{
	int i = 0;
	int* p = (int*)malloc(10 * sizeof(int));
	if (NULL == p)
	{
		exit(EXIT_FAILURE);
	}
	for (i = 0; i <= 10; i++)
	{
		*(p + i) = i;//当i是10的时候越界访问
	}
	free(p);
}
int main()
{
	test();
	return 0;
}

 当i是10的时候越界访问

1.3 对非动态开辟内存使用free释放 

#include<stdio.h>
#include<stdlib.h>
void test()
{
	int a = 10;
	int* p = &a;
	free(p);//ok?
}
int main()
{
	test();
	return 0;
}

 

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

#include<stdio.h>
#include<stdlib.h>
void test()
{
	int* p = (int*)malloc(100);
	p++;
	free(p);//p不再指向动态内存的起始位置
}
int main()
{
	test();
	return 0;
}

 

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

#include<stdio.h>
#include<stdlib.h>
void test()
{
	int* p = (int*)malloc(100);
	free(p);
	free(p);//重复释放
}
int main()
{
	test();
	return 0;
}

 

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

#include<stdio.h>
#include<stdlib.h>
void test()
{
	int* p = (int*)malloc(100);
	if (NULL != p)
	{
		*p = 20;
	}
}
int main()
{
	test();
	while (1);
}
int main()
{
	test();
	return 0;
}

忘记释放不再使用的动态开辟的空间会造成内存泄漏。

切记:
动态开辟的空间一定要释放,并且正确释放

二.经典笔试题

 1.1 题目1

#include<stdio.h>
#include<stdlib.h>
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;
}
请问运行 Test 函数会有什么样的结果?
str指针首先存放了一块空地址即NULL, 再传入函数时函数中创建了心得变量指针p,此时p是str的一份临时拷贝,即p中存放地址NULL,此时再将开辟的空间首地址存放到p指针中,但是随着函数的结束p指针变量会被销毁,此时就会找不到新开辟空间的地址了,所以str中存放的地址仍然是空,系统会报错。

 

 

解决方法:

我们可以讲指针str的地址传进函数,此时函数中需要使用二级指针p来接收str指针的地址, 此时再解引用时相当于将开辟空间的首地址存入str中了。
#include<stdio.h>
#include<stdlib.h>
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;
}

 

 

 

 2.2 题目2

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

请问运行Test 函数会有什么样的结果?

局部数组,出函数之后,p空间就不存在了,内容不知道了

str是页野指针

 

解决方法:

#include<stdio.h>
#include<stdlib.h>
char* GetMemory(void)
{
	char* p = "hello world";
	return p;
}
void Test(void)
{
	char* str = NULL;
	str = GetMemory();
	printf(str);
}
int main()
{
	Test();
	return 0;
}

可将数组改成固定的字符串

 

2.3 题目3 

#include<stdio.h>
#include<stdlib.h>
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;
}
请问运行 Test 函数会有什么样的结果?
没有free函数释放空间会造成内存泄漏

解决方法:

加上free()函数

#include<stdio.h>
#include<stdlib.h>
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);
}
int main()
{
	Test();
	return 0;
}

 

2.4 题目4 

#include<stdio.h>
#include<stdlib.h>
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;
}
请问运行 Test 函数会有什么样的结果?
虽然str指向的空间被释放掉了,但是str没有被置为NULL,此时的str是一个野指针这是一个非常危险的行为

解决方法:

#include<stdio.h>
#include<stdlib.h>
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;
}

 

结语:

每个人的成长都是能力和想要得到的东西,不断匹配的过程,当你的才华和欲望不匹配时,你就该静下心来学习了,如果小编的总结能对你有所帮助,希望小伙伴们三连加关注哦,你的支持是小编创作的最大动力。

 

 

评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旧梦拾遗186

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值