C語言 動態(tài)內(nèi)存分配的詳解及實例
1. 動態(tài)內(nèi)存分配的意義
(1)C 語言中的一切操作都是基于內(nèi)存的。
(2)變量和數(shù)組都是內(nèi)存的別名。
①內(nèi)存分配由編譯器在編譯期間決定
②定義數(shù)組的時候必須指定數(shù)組長度
③數(shù)組長度是在編譯期就必須確定的
(3)但是程序運行的過程中,可能需要使用一些額外的內(nèi)存空間
2. malloc 和 free 函數(shù)
(1)malloc 和 free 用于執(zhí)行動態(tài)內(nèi)存分配的釋放
(2)malloc 所分配的是一塊連續(xù)的內(nèi)存
(3)malloc 以字節(jié)為單位,并且返回值不帶任何的類型信息:void* malloc(size_t size);
(4)free 用于將動態(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 實際分配的內(nèi)存可能有會比請求的多,但不能依賴于不同平臺下的 malloc 行為。
(3)當請求的動態(tài)內(nèi)存無法滿足時,malloc 返回 NULL
(4)當 free 的參數(shù)為 NULL 時,函數(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)會返回一個有效的內(nèi)存地址,大小為1
//但我們不能依賴編譯器的這種行為來使用這個字節(jié)的空間!
p = (int*)malloc(i);
printf("%d ",_msize(p));//返回malloc出來的內(nèi)存空間大小
free(p);
}
return 0;
}
內(nèi)存泄漏檢測模塊
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
//動態(tài)內(nèi)存申請參數(shù)結(jié)構(gòu)體
typedef struct
{
void* pointer;//申請到的內(nèi)存地址
int size; //內(nèi)存塊大小
const char* file; //文件名
int line; //文件行號
}MItem;
static MItem g_record[SIZE]; //記錄每個動態(tài)內(nèi)存申請的操作
void* mallocEx(size_t n, const char* file, const line)
{
int i = 0;
void* ret = malloc(n);//動態(tài)內(nèi)存申請
if(ret != NULL)
{
//申請成功,則記錄下來
//遍歷全局數(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;
//遍歷全局數(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");
//遍歷全局數(shù)組,打印未釋放的空間的申請記錄
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()
{
//沒釋放,會造成內(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.c
E:\Study>a.exe
Potenital 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 個某種類型(如 sizeof(int))大小的內(nèi)存空間。calloc 能以類型大小為單位申請內(nèi)存并初始化為 0.
(3)realloc 用于修改一個原先己經(jīng)分配的內(nèi)存塊大小。當?shù)谝粋€參數(shù) pointer 為 NUL 時,等價于 malloc。
calloc 和 realloc 的使用
#include <stdio.h>
#include <malloc.h>
#define SIZE 5
int 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;
}
通過此文希望大家對C語言的動態(tài)內(nèi)存分配了解掌握,謝謝大家對本站的支持!
相關(guān)文章
C++結(jié)構(gòu)體字節(jié)對齊和共用體大小
這篇文章主要介紹了C++結(jié)構(gòu)體字節(jié)對齊和共用體大小,結(jié)構(gòu)體內(nèi)存對齊在筆試和面試中經(jīng)常被問到,所以這篇文章做個總結(jié),首先通過代碼驗證不同結(jié)構(gòu)體的內(nèi)存大小,需要的朋友可以參考下2021-11-11
關(guān)于AVLTree(C++實現(xiàn))沒有統(tǒng)一旋轉(zhuǎn)操作的問題
這篇文章主要介紹了關(guān)于AVLTree(C++實現(xiàn))沒有統(tǒng)一旋轉(zhuǎn)操作的問題,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02
Visual Studio Code上添加小程序自動補全插件的操作方法
這篇文章主要介紹了Visual Studio Code上添加小程序自動補全插件的操作方法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04
C++中如何將operator==定義為類的成員函數(shù)
這篇文章主要介紹了C++中如何將operator==定義為類的成員函數(shù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01

