当前位置:网站首页>[Study Notes Dish Dog Learning C] Classic Written Exam Questions of Dynamic Memory Management
[Study Notes Dish Dog Learning C] Classic Written Exam Questions of Dynamic Memory Management
2022-08-05 05:16:00 【Jiang Junzhu】
题目一:
- 问题
Please ask the following code,运行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传给GetMemoryThe function is passed the value,所以GetMemory函数的形参pWhat is received is a null pointer.GetMemoryThe function performs dynamic space application internally,地址存放在p中,并不会影响Test函数中的str.所以str的值依旧是NULL,所以strcpy失败.并且当GetMemory函数运行完之后,形参p销毁,dynamically developed100bytes of memory are not freed,这样就会导致内存泄露. - 图解

- 错误更正
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;
}
题目二
- 问题
Please ask the following code,运行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函数内部创建的数组是在栈区上创建的,当函数结束时,This space is also destroyed,因此pArrays have no space,返回pThe address of the array has no real meaning.如果strUse the returned address to access memory,就是非法访问. - 图解

- 错误更正
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;
}
题目三
- 问题
Please ask the following code,运行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的地址,Then dynamically open up a space,Hand over the address of this spacestr,最后拷贝“hello”并打印,没有一点问题,But he did not perform memory release,会导致内存泄漏.图解

错误更正
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;
}
题目四
- 问题
Please ask the following code,运行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;
}
代码分析
The problem with this code is that it doesn't empty,will result in a wild pointer,野指针是非常危险的.当free时,Dynamically opened space has been freed up,只不过strRemember the address of this space,Assigning a value to this space is illegal access.图解

错误更正
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;
}
边栏推荐
- The mall background management system based on Web design and implementation
- Redis哨兵模式配置文件详解
- Shell(4) Conditional Control Statement
- Dephi reverse tool Dede exports function name MAP and imports it into IDA
- "Recursion" recursion concept and typical examples
- Analysis of Mvi Architecture
- Using QR codes to solve fixed asset management challenges
- 开发一套高容错分布式系统
- 逆向理论知识4
- 仪表板展示 | DataEase看中国:数据呈现中国资本市场
猜你喜欢
![[Student Graduation Project] Design and Implementation of the Website Based on the Web Student Information Management System (13 pages)](/img/86/9c9a2541f2b7089ae47e9832fffdb3.png)
[Student Graduation Project] Design and Implementation of the Website Based on the Web Student Information Management System (13 pages)

The solution to the failure to read channel information when dedecms generates a message in the background

Application status of digital twin technology in power system

使用二维码解决固定资产管理的难题

2022 Hangzhou Electric Multi-School 1st Session 01

mutillidae download and installation

Excel Paint

RL强化学习总结(一)

Redis哨兵模式配置文件详解

Flutter真机运行及模拟器运行
随机推荐
【cesium】加载并定位 3D Tileset
「PHP8入门指南」PHP简明介绍
Structured light 3D reconstruction (1) Striped structured light 3D reconstruction
UVA10827
flex布局青蛙游戏通关攻略
MySQL Foundation (1) - Basic Cognition and Operation
社区分享|腾讯海外游戏基于JumpServer构建游戏安全运营能力
Flutter real machine running and simulator running
基于Web的商城后台管理系统的设计与实现
Flutter 父子组件如何都能收到点击事件
Using QR codes to solve fixed asset management challenges
Distributed systems revisited: there will never be a perfect consistency scheme...
【无标题】
结构光三维重建(二)线结构光三维重建
【学生毕业设计】基于web学生信息管理系统网站的设计与实现(13个页面)
Develop a highly fault-tolerant distributed system
u-boot调试定位手段
仪表板展示 | DataEase看中国:数据呈现中国资本市场
A blog clears the Redis technology stack
C language - vernacular to understand the original code, inverse code and complement code