C語言中的malloc使用詳解
一、原型:extern void *malloc(unsigned int num_bytes);
頭文件:#include <malloc.h> 或 #include <alloc.h> (注意:alloc.h 與 malloc.h 的內(nèi)容是完全一致的。)
功能:分配長度為num_bytes字節(jié)的內(nèi)存塊
說明:如果分配成功則返回指向被分配內(nèi)存的指針,否則返回空指針NULL。
當(dāng)內(nèi)存不再使用時,應(yīng)使用free()函數(shù)將內(nèi)存塊釋放。
舉例:
#include<stdio.h> #include<malloc.h> int main() { char *p; p=(char *)malloc(100); if(p) printf("Memory Allocated at: %x/n",p); else printf("Not Enough Memory!/n"); free(p); return 0; }
二、函數(shù)聲明(函數(shù)原型):
void *malloc(int size);
說明:malloc 向系統(tǒng)申請分配指定size個字節(jié)的內(nèi)存空間。返回類型是 void* 類型。void* 表示未確定類型的指針。C,C++規(guī)定,void* 類型可以強制轉(zhuǎn)換為任何其它類型的指針。這個在MSDN上可以找到相關(guān)的解釋,具體內(nèi)容如下:
malloc returns a void pointer to the allocated space, or NULL if there is insufficient memory available. To return a pointer to a type other than void, use a type cast on the return value. The storage space pointed to by the return value is guaranteed to be suitably aligned for storage of any type of object. If size is 0, malloc allocates a zero-length item in the heap and returns a valid pointer to that item. Always check the return from malloc, even if the amount of memory requested is small.
三、malloc與new的不同點
從函數(shù)聲明上可以看出。malloc 和 new 至少有兩個不同: new 返回指定類型的指針,并且可以自動計算所需要大小。比如:
int *p; p = new int; //返回類型為int* 類型(整數(shù)型指針),分配大小為 sizeof(int);
或:
int* parr; parr = new int [100]; //返回類型為 int* 類型(整數(shù)型指針),分配大小為 sizeof(int) * 100;
而 malloc 則必須由我們計算要字節(jié)數(shù),并且在返回后強行轉(zhuǎn)換為實際類型的指針。
int* p; p = (int *) malloc (sizeof(int));
第1、malloc 函數(shù)返回的是 void * 類型,如果你寫成:p = malloc (sizeof(int)); 則程序無法通過編譯,報錯:“不能將 void* 賦值給 int * 類型變量”。所以必須通過 (int *) 來將強制轉(zhuǎn)換。
第2、函數(shù)的實參為 sizeof(int) ,用于指明一個整型數(shù)據(jù)需要的大小。如果你寫成:
int* p = (int *) malloc (1);
代碼也能通過編譯,但事實上只分配了1個字節(jié)大小的內(nèi)存空間,當(dāng)你往里頭存入一個整數(shù),就會有3個字節(jié)無家可歸,而直接“住進鄰居家”!造成的結(jié)果是后面的內(nèi)存中原有數(shù)據(jù)內(nèi)容全部被清空。
malloc 也可以達到 new [] 的效果,申請出一段連續(xù)的內(nèi)存,方法無非是指定你所需要內(nèi)存大小。
比如想分配100個int類型的空間:
int* p = (int *) malloc ( sizeof(int) * 100 ); //分配可以放得下100個整數(shù)的內(nèi)存空間。
另外有一點不能直接看出的區(qū)別是,malloc 只管分配內(nèi)存,并不能對所得的內(nèi)存進行初始化,所以得到的一片新內(nèi)存中,其值將是隨機的。
除了分配及最后釋放的方法不一樣以外,通過malloc或new得到指針,在其它操作上保持一致。
四、動態(tài)申請數(shù)組
申請一維數(shù)組
一維數(shù)組的數(shù)組名可以看成數(shù)組起始元素的首地址,因此我定義一個int *arr的指針,分配n個大小的int型空間,寫法如下:
#include <stdio.h> #include <stdlib.h> int main(void) { int n, *arr; while (scanf("%d", &n) != EOF) { arr = (int *)malloc(sizeof(int) * n); } return 0; }
申請二維數(shù)組
二維數(shù)組的數(shù)組名是其所有一維數(shù)組的首地址,因為二維數(shù)組的數(shù)組名是指針的指針,因為我定義一個row行column列的二維數(shù)組,寫法如下:
#include <stdio.h> #include <stdlib.h> int main(void) { int i, row, column, **arr; while (scanf("%d %d", &row, &column) != EOF) { arr = (int **)malloc(sizeof(int *) * row); // 分配所有行的首地址 for (i = 0; i < row; i ++) { // 按行分配每一列 arr[i] = (int *)malloc(sizeof(int) * column); } free(arr); } return 0; }
總結(jié):
malloc()函數(shù)其實就在內(nèi)存中找一片指定大小的空間,然后將這個空間的首地址范圍給一個指針變量,這里的指針變量可以是一個單獨的指針,也可以是一個數(shù)組的首地址,這要看malloc()函數(shù)中參數(shù)size的具體內(nèi)容。我們這里malloc分配的內(nèi)存空間在邏輯上連續(xù)的,而在物理上可以連續(xù)也可以不連續(xù)。對于我們程序員來說,我們關(guān)注的是邏輯上的連續(xù),因為操作系統(tǒng)會幫我們安排內(nèi)存分配,所以我們使用起來就可以當(dāng)做是連續(xù)的。
相關(guān)文章
C++如何實現(xiàn)BCD碼和ASCII碼的相互轉(zhuǎn)換
這篇文章主要介紹了C++實現(xiàn)BCD碼和ASCII碼互轉(zhuǎn),本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06C++中inet_pton、inet_ntop函數(shù)的用法
這篇文章主要介紹了C++中inet_pton、inet_ntop函數(shù)的用法,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08