C 記憶體管理
C 記憶體管理
本章將講解 C 中的動態記憶體管理。C 語言為記憶體的分配和管理提供了幾個函式。這些函式可以在 <stdlib.h> 標頭檔案中找到。
序號 | 函式和描述 |
---|---|
1 | void *calloc(int num, int size); 在記憶體中動態地分配 num 個長度為 size 的連續空間,並將每一個位元組都初始化為 0。所以它的結果是分配了 num*size 個位元組長度的記憶體空間,並且每個位元組的值都是0。 |
2 | void free(void *address); 該函式釋放 address 所指向的記憶體塊,釋放的是動態分配的記憶體空間。 |
3 | void *malloc(int num); 在堆區分配一塊指定大小的記憶體空間,用來存放資料。這塊記憶體空間在函式執行完成後不會被初始化,它們的值是未知的。 |
4 | void *realloc(void *address, int newsize); 該函式重新分配記憶體,把記憶體擴充套件到 newsize。 |
注意:void * 型別表示未確定型別的指標。C、C++ 規定 void * 型別可以通過型別轉換強制轉換為任何其它型別的指標。
動態分配記憶體
程式設計時,如果您預先知道陣列的大小,那麼定義陣列時就比較容易。例如,一個儲存人名的陣列,它最多容納 100 個字元,所以您可以定義陣列,如下所示:
char name[100];
但是,如果您預先不知道需要儲存的文字長度,例如您想儲存有關一個主題的詳細描述。在這裡,我們需要定義一個指標,該指標指向未定義所需記憶體大小的字元,後續再根據需求來分配記憶體,如下所示:
例項
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char name[100];
char *description;
strcpy(name, "Zara Ali");
/* 動態分配記憶體 */
description = (char *)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 );
}
當上面的程式碼被編譯和執行時,它會產生下列結果:
Name = Zara Ali Description: Zara ali a DPS student in class 10th
上面的程式也可以使用 calloc() 來編寫,只需要把 malloc 替換為 calloc 即可,如下所示:
calloc(200, sizeof(char));
當動態分配記憶體時,您有完全控制權,可以傳遞任何大小的值。而那些預先定義了大小的陣列,一旦定義則無法改變大小。
重新調整記憶體的大小和釋放記憶體
當程式退出時,作業系統會自動釋放所有分配給程式的記憶體,但是,建議您在不需要記憶體時,都應該呼叫函式 free() 來釋放記憶體。
或者,您可以通過呼叫函式 realloc() 來增加或減少已分配的記憶體塊的大小。讓我們使用 realloc() 和 free() 函式,再次檢視上面的例項:
例項
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char name[100];
char *description;
strcpy(name, "Zara Ali");
/* 動態分配記憶體 */
description = (char *)malloc( 30 * sizeof(char) );
if( description == NULL )
{
fprintf(stderr, "Error - unable to allocate required memory\n");
}
else
{
strcpy( description, "Zara ali a DPS student.");
}
/* 假設您想要儲存更大的描述資訊 */
description = (char *) 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() 函式釋放記憶體 */
free(description);
}
當上面的程式碼被編譯和執行時,它會產生下列結果:
Name = Zara Ali Description: Zara ali a DPS student.She is in class 10th
您可以嘗試一下不重新分配額外的記憶體,strcat() 函式會生成一個錯誤,因為儲存 description 時可用的記憶體不足。