当前位置:网站首页>【学习笔记之菜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;
}
边栏推荐
- Detailed explanation of Mysql's undo log
- There are several common event handling methods in Swing?How to listen for events?
- The solution to the failure to read channel information when dedecms generates a message in the background
- 多御安全浏览器新版下载 | 功能优秀性能出众
- How to deal with DNS hijacking?
- 什么是ASEMI光伏二极管,光伏二极管的作用
- App快速开发建设心得:小程序+自定义插件的重要性
- Analyses the mainstream across technology solutions
- 说说数据治理中常见的20个问题
- Application status of digital twin technology in power system
猜你喜欢

In the hot summer, teach you to use Xiaomi smart home accessories + Raspberry Pi 4 to connect to Apple HomeKit
![[MRCTF2020]PYWebsite](/img/d4/57e8e5ee45b742894679f3f5671516.png)
[MRCTF2020]PYWebsite

什么是ASEMI光伏二极管,光伏二极管的作用

请写出SparkSQL语句

What is the function of industrial-grade remote wireless transmission device?

Mysql的undo log详解

UE4 后期处理体积 (角色受到伤害场景颜色变淡案例)

How do newcomers get started and learn software testing?

【8.4】代码源 - 【数学】【历法】【删库】【不朴素的数列(Bonus)】

Qixi Festival code confession
随机推荐
C+ +核心编程
[Nine Lectures on Backpacks - 01 Backpack Problems]
how to measure distance from point to face in creo
UE4 通过与其它Actor互动开门
write the story about us
The production method of the powered small sailboat is simple, the production method of the electric small sailboat
DEJA_VU3D - Cesium功能集 之 058-高德地图纠偏
Paparazzi: Surface Editing by way of Multi-View Image Processing
将故事写成我们
大学物理---质点运动学
4T硬盘剩余很多提示“No space left on device“磁盘空间不足
1007 Climb Stairs (greedy | C thinking)
[Surveying] Quick Summary - Excerpt from Gaoshu Gang
Four-digit display header design
[Geek Challenge 2019]FinalSQL
Ali's local life's single-quarter revenue is 10.6 billion, Da Wenyu's revenue is 7.2 billion, and Cainiao's revenue is 12.1 billion
AUTOCAD——标注关联
[BSidesCF 2019] Kookie
Mysql的undo log详解
UI自动化测试 App的WebView页面中,当搜索栏无搜索按钮时处理方法