C說話 靜態內存分派的詳解及實例。本站提示廣大學習愛好者:(C說話 靜態內存分派的詳解及實例)文章只能為提供參考,不一定能成為您想要的結果。以下是C說話 靜態內存分派的詳解及實例正文
1. 靜態內存分派的意義
(1)C 說話中的一切操作都是基於內存的。
(2)變量和數組都是內存的別號。
①內存分派由編譯器在編譯時代決議
②界說數組的時刻必需指定命組長度
③數組長度是在編譯期就必需肯定的
(3)然則法式運轉的進程中,能夠須要應用一些額定的內存空間
2. malloc 和 free 函數
(1)malloc 和 free 用於履行靜態內存分派的釋放
(2)malloc 所分派的是一塊持續的內存
(3)malloc 以字節為單元,而且前往值不帶任何的類型信息:void* malloc(size_t size);
(4)free 用於將靜態內存清償體系:void free(void* pointer);
(5)_msize(void* pointer)可以獲得 malloc 出來的內存空間年夜小
3. 應用 malloc 和 free 須要留意的處所
(1)malloc 和 free 是庫函數,而不是體系挪用
(2)malloc 現實分派的內存能夠有會比要求的多,但不克不及依附於分歧平台下的 malloc 行動。
(3)當要求的靜態內存沒法知足時,malloc 前往 NULL
(4)當 free 的參數為 NULL 時,函數直接前往
malloc(0)前往甚麼?
#include <stdio.h>
#include <malloc.h>
int main()
{
int i=10;
int* p= NULL;
for(i=0;i<100;i++)
{
//留意,malloc(0)會前往一個有用的內存地址,年夜小為1
//但我們不克不及依附編譯器的這類行動來應用這個字節的空間!
p = (int*)malloc(i);
printf("%d ",_msize(p));//前往malloc出來的內存空間年夜小
free(p);
}
return 0;
}
內存洩露檢測模塊
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
復制代碼
#include "mleak.h"
#define SIZE 256
//靜態內存請求參數構造體
typedef struct
{
void* pointer;//請求到的內存地址
int size; //內存塊年夜小
const char* file; //文件名
int line; //文件行號
}MItem;
static MItem g_record[SIZE]; //記載每一個靜態內存請求的操作
void* mallocEx(size_t n, const char* file, const line)
{
int i = 0;
void* ret = malloc(n);//靜態內存請求
if(ret != NULL)
{
//請求勝利,則記載上去
//遍歷全局數組,記載此次操作
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;
//遍歷全局數組,釋放內存空間,並消除操作記載
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");
//遍歷全局數組,打印未釋放的空間的請求記載
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()
{
//沒釋放,會形成內存洩露!
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;
}
/*
輸入成果:
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 函數
(1)malloc 的同胞兄弟:
void* calloc(size_t num, size_t size);
void* realloc(void* pointer,size_t new_size);
(2)calloc 參數表現要前往 num 個某品種型(如 sizeof(int))年夜小的內存空間。calloc 能以類型年夜小為單元請求內存並初始化為 0.
(3)realloc 用於修正一個本來己經分派的內存塊年夜小。當第一個參數 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內存沒有初始化
short* pS = (short*)calloc(SIZE, sizeof(short));//內存初始化為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); //重置內存年夜小之前的pI指針
pI = (int*)realloc(pI, 2 * SIZE * sizeof(int)); //內存未初始化的
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說話的靜態內存分派懂得控制,感謝年夜家對本站的支撐!