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。 |
動態分配內存
編程時,如果您預先知道數組的大小,那麼定義數組時就比較容易。例如,一個存儲人名的數組,它最多容納 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 = 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 = 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 = 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 時可用的內存不足。