当前位置:网站首页>【学习笔记之菜Dog学C】动态内存管理之经典笔试题
【学习笔记之菜Dog学C】动态内存管理之经典笔试题
2022-08-05 04:26:00 【姜君竹】
题目一:
- 问题
请问下面这段代码,运行Test 函数会有什么样的结果?
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;
}
- 代码分析
str传给GetMemory函数的时候传递的是值,所以GetMemory函数的形参p接收的是一个空指针。GetMemory函数在内部进行动态空间申请,地址存放在p中,并不会影响Test函数中的str。所以str的值依旧是NULL,所以strcpy失败。并且当GetMemory函数运行完之后,形参p销毁,而动态开辟的100个字节内存并没有释放,这样就会导致内存泄露。 - 图解

- 错误更正
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;
}
题目二
- 问题
请问下面这段代码,运行Test 函数会有什么样的结果?
char* GetMemory(void) {
char p[] = "hello world";
return p;
}
void Test(void) {
char* str = NULL;
str = GetMemory();
printf(str);
}
int main() {
Test();
return 0;
}
- 代码分析
GetMemory函数内部创建的数组是在栈区上创建的,当函数结束时,这块空间也就销毁了,因此p数组是没有空间的,返回p数组的地址没有实际意义。如果str通过返回的地址去访问内存,就是非法访问。 - 图解

- 错误更正
char* GetMemory(void) {
char* p = (char*)malloc(15);
p = "hello world";
return p;
}
void Test(void) {
char* str = NULL;
str = GetMemory();
printf(str);
}
int main() {
Test();
return 0;
}
题目三
- 问题
请问下面这段代码,运行Test 函数会有什么样的结果?
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;
}
代码分析
GetMemory函数获取str的地址,然后动态开辟一块空间,把这块空间的地址交给str,最后拷贝“hello”并打印,没有一点问题,但是他没有进行内存释放,会导致内存泄漏。图解

错误更正
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;
}
题目四
- 问题
请问下面这段代码,运行Test 函数会有什么样的结果?
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时,动态开辟的空间已经被释放了,只不过str还记得这块空间的地址,在给这块空间赋值属于是非法访问。图解

错误更正
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;
}
边栏推荐
- Mini Program_Dynamic setting of tabBar theme skin
- [Surveying] Quick Summary - Excerpt from Gaoshu Gang
- UE4 为子弹蓝图添加声音和粒子效果
- UE4 在游戏运行时更改变量 (通过鼠标滑轮来更改第一人称角色的最大行走速度)
- Talk about 20 common problems in data governance
- DEJA_VU3D - Cesium功能集 之 058-高德地图纠偏
- upload upload pictures to Tencent cloud, how to upload pictures
- [BSidesCF 2019]Kookie
- write the story about us
- 四位数显表头设计
猜你喜欢

How to solve the three major problems of bank data collection, data supplementary recording and index management?

Mysql的undo log详解

【测量学】速成汇总——摘录高数帮
![[BJDCTF2020]EasySearch](/img/60/464de3bcdda876171b9f61ad31bff1.png)
[BJDCTF2020]EasySearch

UE4 更改组件变量 (以修改第一人称角色模板的最大行走速度和跳跃高度为例)

UE4 第一人称角色模板 添加生命值和调试伤害

creo怎么测量点到面的距离

四位数显表头设计

Detailed explanation of each module of ansible

The solution to the failure to read channel information when dedecms generates a message in the background
随机推荐
In the WebView page of the UI automation test App, the processing method when the search bar has no search button
8.04 Day35-----MVC三层架构
DNS被劫持如何处理?
关于#SQL#的迭代、父子结构查询问题,如何解决?
[CISCN2019 华东南赛区]Web11
JeeSite新建报表
请写出SparkSQL语句
how to measure distance from point to face in creo
Visibility of multi-column attribute column elements: display, visibility, opacity, vertical alignment: vertical-align, z-index The larger it is, the more it will be displayed on the upper layer
Is the NPDP certificate high in gold content?Compared to PMP?
The production method of the powered small sailboat is simple, the production method of the electric small sailboat
Cron(Crontab)--use/tutorial/example
[MRCTF2020] PYWebsite
[informix] Resolving startup errors and solutions
七夕节赚徽章拉
[CISCN2019 South China Division]Web11
Day14 jenkins deployment
Bytebuffer put flip compact clear method demonstration
【informix】解决启动报错大全,以及解决办法
Detailed explanation of each module of ansible