动态内存管理之经典笔试题

目录

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

题目1

题目2

题目3

题目4


今天我们来接着讲几道经典的笔试题。首先来了解一下c\c++程序的内存开辟,使我们做题的头脑更加清晰。

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

  • 内核空间 是用户代码不能读写的
  • 栈区是用来开辟 局部变量 形式参数,函数栈帧的创建与销毁均在栈区上。
  • 戳一戳【函数栈帧的创建与销毁】函数栈帧的创建与销毁-CSDN博客
  • 堆区 是用来开辟动态内存 malloc calloc realloc
  • 数据段 即静态区 是用来存放全局数据和静态数据
  • 代码段  即用户写的代码都会翻译成二进制指令放在代码段,不能被改写
  • 内存映射区 在我们的操作系统课程会讲到

C/C++程序内存分配的几个区域:

  1. 栈区(stack):在执行函数时,函数内局部变量的存储单元都可以在栈上创建,函数执行结束时这些存储单元自动被释放。栈内存分配运算内置于处理器的指令集中,效率很高,但是分配的内存容量有限。 栈区主要存放运行函数而分配的局部变量、函数参数、返回数据、返回地址等。
  2. 堆区(heap):一般由程序员分配释放, 若程序员不释放,程序结束时可能由OS回收。分配方式类似于链表。
  3. 数据段(静态区)(static)存放全局变量、静态数据。程序结束后由系统释放。
  4.  代码段:存放函数体(类成员函数和全局函数)的二进制代码。 
  5. 有了这幅图,我们就可以更好的理解在《C语言初识》中讲的static关键字修饰局部变量的例子了。实际上普通的局部变量是在栈区分配空间的,栈区的特点是在上面创建的变量出了作用域就销毁。但是被static修饰的变量存放在数据段(静态区),数据段的特点是在上面创建的变量,直到程序结束才销毁,所以生命周期变。

学习了【函数栈帧的创建与销毁】是更容易理解哦

下面题目我们讲从考点 题目代码本身 注意点 修改去解释。 

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

题目1

考点:
1.形参是实参的一份临时拷贝
2.内存泄漏
3.堆区的内存创建与销毁
#include<stdio.h>
#include<string.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);
    //关于字符串的打印点
    printf("hhello word\n");//"hello word"表示的是字符串首元素的地址
    char*p="hello word\n";//表示字符串首元素地址
    printf(p);//这样子也是可以的
}
int main()
{
	Test();
	return 0;
}

  • 形参是实参的一份临时拷贝,修改形式参数并不能修改实际参数(除非传指针)
  • 形式参数p出了函数就销毁了,此刻找不到malloc函数开辟的空间
  • malloc函数开辟的空间没有及时释放,也没有机会释放
  • 动态内存开辟的空间需要及时释放,自己使用自己释放,即便忘记释放,也需要告诉别人释 放,且给别人机会释放(找得到这块申请空间的地址)

【修改方案一 】


  •  传地址:可以修改形式参数。
  •  及时释放,置为空指针
#include<stdio.h>
#include<string.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);
	free(str);
	str = NULL;
}
int main()
{
	Test();
	return 0;
}

【修改方案二】


  •  返回malloc开辟的空间的地址
  •  及时释放,置为空指针
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
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;
}

题目2

考点:返回栈空间地址
1.栈区的内存创建与销毁
2.野指针
#include<stdio.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;
}

  • 在栈区上,函数结束调用,函数栈帧就销毁了,没有权利访问了
  • 在堆区上,只有free释放才能销毁开辟的内存 

✔当然,还存在一个问题,很多人很容易混淆,【栈空间地址 】和【栈空间变量】的问题。

对比下面两端代码。你发现了什么?? 

//返回栈空间变量
#include<stdio.h>
int* test()
{
	int a = 10;
	return &a;
}
int main()
{
	int* p = test();
	printf("%d\n", *p);
	return 0;
}
//返回栈空间地址
#include<stdio.h>
int* test()
{
	int a = 10;
	return a;
}
int main()
{
	int p = test();
	printf("%d\n", p);
	return 0;
}

 

题目3

考点:内存泄露
#include<stdio.h>
#include<string.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;
}

 【修改方案】


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

题目4

考点:非法访问,野指针
#include<stdio.h>
#include<string.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;
}

  • 注意free和动态内存函数是配套使用
  • free完之后,一定要将指针变量置为空指针NULL,避免非法访问的问题 

【修改方案】 


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

 学习真的得重复做。我们加油敲代码哈。做【高质量C\C++】这本书的题目!!

✔✔✔✔✔最后,感谢大家的阅读,若有错误和不足,欢迎指正!下篇博文我们讲解【柔性数组】。

代码------→【gitee:唐棣棣 (TSQXG) - Gitee.com

联系------→【邮箱:2784139418@qq.com】

  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

唐唐思

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

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

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

打赏作者

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

抵扣说明:

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

余额充值