本章將講解 C 中的動(dòng)態(tài)內(nèi)存管理。C 語(yǔ)言為內(nèi)存的分配和管理提供了幾個(gè)函數(shù)。這些函數(shù)可以在 <stdlib.h> 頭文件中找到。
序號(hào) | 函數(shù)和描述 |
---|---|
1 | void *calloc(int num, int size); 該函數(shù)分配一個(gè)帶有 function allocates an array of num 個(gè)元素的數(shù)組,每個(gè)元素的大小為 size 字節(jié)。 |
2 | void free(void *address); 該函數(shù)釋放 address 所指向的h內(nèi)存塊。 |
3 | void *malloc(int num); 該函數(shù)分配一個(gè) num 字節(jié)的數(shù)組,并把它們進(jìn)行初始化。 |
4 | void *realloc(void *address, int newsize); 該函數(shù)重新分配內(nèi)存,把內(nèi)存擴(kuò)展到 newsize。 |
編程時(shí),如果您預(yù)先知道數(shù)組的大小,那么定義數(shù)組時(shí)就比較容易。例如,一個(gè)存儲(chǔ)人名的數(shù)組,它最多容納 100 個(gè)字符,所以您可以定義數(shù)組,如下所示:
char name[100];
但是,如果您預(yù)先不知道需要存儲(chǔ)的文本長(zhǎng)度,例如您想存儲(chǔ)有關(guān)一個(gè)主題的詳細(xì)描述。在這里,我們需要定義一個(gè)指針,該指針指向未定義所需內(nèi)存大小的字符,后續(xù)再根據(jù)需求來(lái)分配內(nèi)存,如下所示:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char name[100];
char *description;
strcpy(name, "Zara Ali");
/* 動(dòng)態(tài)分配內(nèi)存 */
description = malloc( 200 * sizeof(char) );
if( description == NULL ){
fprintf(stderr, "Error - unable to allocate required memory\n");
}
else{
strcpy( description, "Zara ali a DPS student in class 10th");
}
printf("Name = %s\n", name );
printf("Description: %s\n", description );
}
當(dāng)上面的代碼被編譯和執(zhí)行時(shí),它會(huì)產(chǎn)生下列結(jié)果:
Name = Zara Ali Description: Zara ali a DPS student in class 10th
上面的程序也可以使用 calloc() 來(lái)編寫,只需要把 malloc 替換為 calloc 即可,如下所示:
calloc(200, sizeof(char));
當(dāng)動(dòng)態(tài)分配內(nèi)存時(shí),您有完全控制權(quán),可以傳遞任何大小的值。而那些預(yù)先定義了大小的數(shù)組,一旦定義則無(wú)法改變大小。
當(dāng)程序退出時(shí),操作系統(tǒng)會(huì)自動(dòng)釋放所有分配給程序的內(nèi)存,但是,建議您在不需要內(nèi)存時(shí),都應(yīng)該調(diào)用函數(shù) free() 來(lái)釋放內(nèi)存。
或者,您可以通過(guò)調(diào)用函數(shù) realloc() 來(lái)增加或減少已分配的內(nèi)存塊的大小。讓我們使用 realloc() 和 free() 函數(shù),再次查看上面的實(shí)例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char name[100];
char *description;
strcpy(name, "Zara Ali");
/* 動(dòng)態(tài)分配內(nèi)存 */
description = malloc( 30 * sizeof(char) );
if( description == NULL ){
fprintf(stderr, "Error - unable to allocate required memory\n");
}
else{
strcpy( description, "Zara ali a DPS student.");
}
/* 假設(shè)您想要存儲(chǔ)更大的描述信息 */
description = realloc( description, 100 * sizeof(char) );
if( description == NULL ){
fprintf(stderr, "Error - unable to allocate required memory\n");
}
else{
strcat( description, "She is in class 10th");
}
printf("Name = %s\n", name );
printf("Description: %s\n", description );
/* 使用 free() 函數(shù)釋放內(nèi)存 */
free(description);
}
當(dāng)上面的代碼被編譯和執(zhí)行時(shí),它會(huì)產(chǎn)生下列結(jié)果:
Name = Zara Ali Description: Zara ali a DPS student.She is in class 10th
您可以嘗試一下不重新分配額外的內(nèi)存,strcat() 函數(shù)會(huì)生成一個(gè)錯(cuò)誤,因?yàn)榇鎯?chǔ) description 時(shí)可用的內(nèi)存不足。
更多建議: