網站首頁 編程語言 正文
前言
當我們用動態內存分配函數來編寫程序時,在編寫的過程中常常會產生一些不易被察覺,被發現的錯誤,例如對NULL指針的解引用操作,對動態開辟空間的越界訪問,使用free釋放非動態開辟的空間,使用free釋放動態內存中的一部分,對同一塊動態開辟的空間,多次釋放,動態開辟空間忘記釋放。下面我們挨個來分析,刨析一下這些個常見的動態內存開辟的問題。
一、動態內存錯誤
1.對NULL指針的解引用操作
代碼如下(示例):
錯誤示例: //動態內存開辟 int main() { int* p = malloc(100000000000); //沒有對mollac函數的返回值做判空處理 int i = 0; for (i = 0; i < 10; i++) { *(p + i) = 5; } return 0; } 正確示例: //動態內存開辟 int main() { int* p = malloc(100000000000); if (p == NULL) { return 1; } int i = 0; for (i = 0; i < 10; i++) { *(p + i) = 5; } for (i = 0; i < 10; i++) { printf("%d ", p[i]); } free(p); p = NULL; return 0; }
2.對動態開辟空間的越界訪問
代碼如下(示例):
錯誤示例: //動態內存開辟 int main() { int* p = (int*)malloc(10*sizeof(int)); if (p == NULL) { return 1; } int i = 0; //越界訪問 for (i = 0; i < 40; i++)//malloc函數只是開辟了十個整型的空間,這里卻要訪問四十個元素。 { *(p + i) = 5; } for (i = 0; i < 40; i++) { printf("%d ", p[i]); } free(p); p = NULL; return 0; } 正確示例: //動態內存開辟 int main() { int* p = (int*)malloc(10*sizeof(int)); if (p == NULL) { return 1; } int i = 0; for (i = 0; i < 10; i++) *(p + i) = 5; } for (i = 0; i < 10; i++) { printf("%d ", p[i]); } free(p); p = NULL; return 0; }
3.使用free釋放非動態開辟的空間
代碼如下(示例):
//動態內存開辟 int main() { int arr[10] = { 0 };//棧區 int* p = arr; free(p);//使用free釋放非動態開辟的空間 p = NULL; return 0; }
4.使用free釋放動態內存中的一部分
代碼如下(示例):
//動態內存開辟 int main() { int* p = malloc(10 * sizeof(int)); if (p == NULL) { return 1; } int i = 0; for (i = 0; i < 5; i++) { *p++ = i;//1:p一直往后走之后沒人知道起始空間的位置在哪,2:p釋放的只是后面空間的一部分,前面的空間并沒有得到釋放。 } free(p); p = NULL; return 0; }
5.對同一塊動態內存動態開辟的空間多次釋放
代碼如下(示例):
//動態內存開辟 int main() { int* p = malloc(10 * sizeof(int)); //使用 //釋放 free(p); //再次釋放 free(p);//free要是傳的是空指針什么事都不會發生。 p = NULL; return 0; }
6.動態開辟的空間忘記釋放(容易造成內存泄露,比較嚴重)
代碼如下(示例):
void test() { int* p = malloc(10 * sizeof(int)); if (p == NULL) { return 1; } //使用 //忘記釋放 } //動態內存開辟 int main() { test(); return 0; }
二、動態內存錯誤面試題分析
1.NULL指針傳參不取地址傳的也是一份臨時拷貝
例題分析: 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是str的一份臨時拷貝。
在GetMemory函數內部動態申請空間的地址,存放在P中,不會影響外面str,所以當GetMemory函數返回
之后,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; } //第二種改法: char* 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; }
2.局部變量和形式參數存在于棧上
代碼如下(示例):
//例題分析: 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的數組的空間就還給了操作系統
返回的地址是沒有實際意義的,如果通過返回的地址,去訪問內存就是非法訪問內存。
?正確代碼:
char* GetMemory(void) { static char p[] = "hello world"; return p; } void test(void) { char* str = NULL; str = GetMemory(); printf(str); } int main() { test(); return 0; }
3.動態內存開的空間記得free釋放掉
代碼如下(示例)
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; }
錯誤分析:
申請的動態內存空間使用完之后沒有及時free釋放掉。
正確代碼:
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.非法訪問內存
代碼如下(示例)
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還記得這塊空間的起始地址,但是也不能訪問,屬于非法訪問內存空間。
free完之后要及時把str置成NULL指針。
正確代碼:
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; }
總結:
上述給大家簡單介紹了動態內存開辟常見的幾種問題,也分析了往年的幾道面試題里面的錯誤,讓我們加深了對這一章的理解,后續自己使用的時候可以有效的規避掉這些問題。相信大家都學會了。如果上述文章有任何問題?,歡迎大佬們提出質疑,我會虛心學習和改正,最重要的是能共同進步,共同成長,學習好編程。
原文鏈接:https://blog.csdn.net/m0_64397675/article/details/122942990
相關推薦
- 2022-03-27 MongoDB4.28開啟權限認證配置用戶密碼登錄功能_MongoDB
- 2023-01-10 Flutter圖片緩存管理ImageCache原理分析_Android
- 2023-08-16 uniapp中v-model數據無法讀取問題 failed for prop “value“
- 2022-10-14 使用docker起一個verdaccio的容器
- 2023-02-02 C語言中的直接插入排序(帶圖詳細)_C 語言
- 2022-12-28 C++關鍵字const使用方法詳解_C 語言
- 2022-07-31 C語言數據結構算法基礎之循環隊列示例_C 語言
- 2023-03-15 C語言枚舉與聯合圖文梳理講解_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支