C語(yǔ)言 動(dòng)態(tài)內(nèi)存分配的詳解及實(shí)例
1. 動(dòng)態(tài)內(nèi)存分配的意義
(1)C 語(yǔ)言中的一切操作都是基于內(nèi)存的。
(2)變量和數(shù)組都是內(nèi)存的別名。
①內(nèi)存分配由編譯器在編譯期間決定
②定義數(shù)組的時(shí)候必須指定數(shù)組長(zhǎng)度
③數(shù)組長(zhǎng)度是在編譯期就必須確定的
(3)但是程序運(yùn)行的過(guò)程中,可能需要使用一些額外的內(nèi)存空間
2. malloc 和 free 函數(shù)
(1)malloc 和 free 用于執(zhí)行動(dòng)態(tài)內(nèi)存分配的釋放
(2)malloc 所分配的是一塊連續(xù)的內(nèi)存
(3)malloc 以字節(jié)為單位,并且返回值不帶任何的類(lèi)型信息:void* malloc(size_t size);
(4)free 用于將動(dòng)態(tài)內(nèi)存歸還系統(tǒng):void free(void* pointer);
(5)_msize(void* pointer)可以獲取 malloc 出來(lái)的內(nèi)存空間大小
3. 使用 malloc 和 free 需要注意的地方
(1)malloc 和 free 是庫(kù)函數(shù),而不是系統(tǒng)調(diào)用
(2)malloc 實(shí)際分配的內(nèi)存可能有會(huì)比請(qǐng)求的多,但不能依賴(lài)于不同平臺(tái)下的 malloc 行為。
(3)當(dāng)請(qǐng)求的動(dòng)態(tài)內(nèi)存無(wú)法滿足時(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
//但我們不能依賴(lài)編譯器的這種行為來(lái)使用這個(gè)字節(jié)的空間!
p = (int*)malloc(i);
printf("%d ",_msize(p));//返回malloc出來(lái)的內(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)成功,則記錄下來(lái)
//遍歷全局?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()
{
//沒(méi)釋放,會(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.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 個(gè)某種類(lèi)型(如 sizeof(int))大小的內(nèi)存空間。calloc 能以類(lèi)型大小為單位申請(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 5
int main()
{
int i = 0;
int* pI = (int*)malloc(SIZE * sizeof(int)); //malloc內(nèi)存沒(mé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;
}
通過(guò)此文希望大家對(duì)C語(yǔ)言的動(dòng)態(tài)內(nèi)存分配了解掌握,謝謝大家對(duì)本站的支持!
相關(guān)文章
C++結(jié)構(gòu)體字節(jié)對(duì)齊和共用體大小
這篇文章主要介紹了C++結(jié)構(gòu)體字節(jié)對(duì)齊和共用體大小,結(jié)構(gòu)體內(nèi)存對(duì)齊在筆試和面試中經(jīng)常被問(wèn)到,所以這篇文章做個(gè)總結(jié),首先通過(guò)代碼驗(yàn)證不同結(jié)構(gòu)體的內(nèi)存大小,需要的朋友可以參考下2021-11-11
關(guān)于AVLTree(C++實(shí)現(xiàn))沒(méi)有統(tǒng)一旋轉(zhuǎn)操作的問(wèn)題
這篇文章主要介紹了關(guān)于AVLTree(C++實(shí)現(xiàn))沒(méi)有統(tǒng)一旋轉(zhuǎn)操作的問(wèn)題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02
Visual Studio Code上添加小程序自動(dòng)補(bǔ)全插件的操作方法
這篇文章主要介紹了Visual Studio Code上添加小程序自動(dòng)補(bǔ)全插件的操作方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04
C++中如何將operator==定義為類(lèi)的成員函數(shù)
這篇文章主要介紹了C++中如何將operator==定義為類(lèi)的成員函數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01

