1. 動(dòng)態(tài)內(nèi)存分配的意義
(1)C 語言中的一切操作都是基于內(nèi)存的。
(2)變量和數(shù)組都是內(nèi)存的別名。
①內(nèi)存分配由編譯器在編譯期間決定
②定義數(shù)組的時(shí)候必須指定數(shù)組長(zhǎng)度
③數(shù)組長(zhǎng)度是在編譯期就必須確定的
(3)但是程序運(yùn)行的過程中,可能需要使用一些額外的內(nèi)存空間
2. malloc 和 free 函數(shù)
(1)malloc 和 free 用于執(zhí)行動(dòng)態(tài)內(nèi)存分配的釋放
(2)malloc 所分配的是一塊連續(xù)的內(nèi)存
(3)malloc 以字節(jié)為單位,并且返回值不帶任何的類型信息:void* malloc(size_t size);
(4)free 用于將動(dòng)態(tài)內(nèi)存歸還系統(tǒng):void free(void* pointer);
(5)_msize(void* pointer)可以獲取 malloc 出來的內(nèi)存空間大小
3. 使用 malloc 和 free 需要注意的地方
(1)malloc 和 free 是庫函數(shù),而不是系統(tǒng)調(diào)用
(2)malloc 實(shí)際分配的內(nèi)存可能有會(huì)比請(qǐng)求的多,但不能依賴于不同平臺(tái)下的 malloc 行為。
(3)當(dāng)請(qǐng)求的動(dòng)態(tài)內(nèi)存無法滿足時(shí),malloc 返回 NULL
(4)當(dāng) free 的參數(shù)為 NULL 時(shí),函數(shù)直接返回
malloc(0)返回什么?
#include <stdio.h>#include <malloc.h>int main(){ int i=10; int* p= NULL; for(i=0;i<100;i++) { //注意,malloc(0)會(huì)返回一個(gè)有效的內(nèi)存地址,大小為1 //但我們不能依賴編譯器的這種行為來使用這個(gè)字節(jié)的空間! p = (int*)malloc(i); printf("%d ",_msize(p));//返回malloc出來的內(nèi)存空間大小 free(p); } return 0;}內(nèi)存泄漏檢測(cè)模塊
mleak.h
#ifndef _MLEAK_H_#define _MLEAK_H_#include <stdio.h>#include <malloc.h>#define MALLOC(n) mallocEx(n, __FILE__, __LINE__)#define FREE(p) freeEx(p)void* mallocEx(size_t n, const char* file, const line);void freeEx(void* p);void PRINT_LEAK_INFO();#endif
mleak.c
復(fù)制代碼
#include "mleak.h"#define SIZE 256//動(dòng)態(tài)內(nèi)存申請(qǐng)參數(shù)結(jié)構(gòu)體typedef struct{ void* pointer;//申請(qǐng)到的內(nèi)存地址 int size; //內(nèi)存塊大小 const char* file; //文件名 int line; //文件行號(hào)}MItem;static MItem g_record[SIZE]; //記錄每個(gè)動(dòng)態(tài)內(nèi)存申請(qǐng)的操作void* mallocEx(size_t n, const char* file, const line){ int i = 0; void* ret = malloc(n);//動(dòng)態(tài)內(nèi)存申請(qǐng) if(ret != NULL) { //申請(qǐng)成功,則記錄下來 //遍歷全局?jǐn)?shù)組,記錄此次操作 for(i = 0; i< SIZE; i++) { //查找位置 if(g_record[i].pointer == NULL) { g_record[i].pointer = ret; g_record[i].size = n; g_record[i].file = file; g_record[i].line = line; break; } } } return ret;}void freeEx(void* p){ if(p != NULL) { int i = 0; //遍歷全局?jǐn)?shù)組,釋放內(nèi)存空間,并清除操作記錄 for(i = 0; i< SIZE; i++) { if(g_record[i].pointer == p) { g_record[i].pointer = NULL; g_record[i].size = 0; g_record[i].file = NULL; g_record[i].line = 0; free(p); break; } } }}void PRINT_LEAK_INFO(){ int i = 0; printf("Potenital Memory Leak Info:/n"); //遍歷全局?jǐn)?shù)組,打印未釋放的空間的申請(qǐng)記錄 for(i = 0; i< SIZE; i++) { //查找位置 if(g_record[i].pointer != NULL) { printf("Address:%p, size:%d, Location:%s:%d/n", g_record[i].pointer, g_record[i].size, g_record[i].file, g_record[i].line); } }}testc.
#include <stdio.h>#include "mleak.h"void f(){ //沒釋放,會(huì)造成內(nèi)存泄漏! MALLOC(100); }int main(){ int* p = (int*)MALLOC(3 * sizeof(int)); f(); p[0] = 1; p[1] = 2; p[2] = 3; FREE(p); PRINT_LEAK_INFO(); return 0;}/*輸出結(jié)果:E:/Study>gcc test.c mleak.cE:/Study>a.exePotenital Memory Leak Info:Address:00602ED8, size:100, Location:38-1.c:7*/4. calloc 和 realloc 函數(shù)
(1)malloc 的同胞兄弟:
void* calloc(size_t num, size_t size);
void* realloc(void* pointer,size_t new_size);
(2)calloc 參數(shù)表示要返回 num 個(gè)某種類型(如 sizeof(int))大小的內(nèi)存空間。calloc 能以類型大小為單位申請(qǐng)內(nèi)存并初始化為 0.
(3)realloc 用于修改一個(gè)原先己經(jīng)分配的內(nèi)存塊大小。當(dāng)?shù)谝粋€(gè)參數(shù) pointer 為 NUL 時(shí),等價(jià)于 malloc。
calloc 和 realloc 的使用
#include <stdio.h>#include <malloc.h>#define SIZE 5int main(){ int i = 0; int* pI = (int*)malloc(SIZE * sizeof(int)); //malloc內(nèi)存沒有初始化 short* pS = (short*)calloc(SIZE, sizeof(short));//內(nèi)存初始化為0 for (i = 0; i < SIZE;i++) { printf("pI[%d] = %d, pS[%d] = %d/n", i, pI[i], i, pS[i]); } printf("Before: pI = %p/n", pI); //重置內(nèi)存大小之前的pI指針 pI = (int*)realloc(pI, 2 * SIZE * sizeof(int)); //內(nèi)存未初始化的 printf("After: pI = %p/n", pI); for (i = 0; i < 10;i++) { printf("pI[%d] = %d/n", i, pI[i]); } free(pI); free(pS); return 0;}通過此文希望大家對(duì)C語言的動(dòng)態(tài)內(nèi)存分配了解掌握,謝謝大家對(duì)本站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選