C内存管理
C 内存管理 ¶
本章将讲解 C 中的动态内存管理。C 语言为内存的分配和管理提供了几个函数。这些函数可以在 <stdlib.h> 头文件中找到。
函数 | 描述 |
---|---|
void *calloc(int num, int size) | 在内存中动态地分配 num 个长度为 size 的连续空间,并将每一个字节都初始化为 0。所以它的结果是分配了 num*size 个字节长度的内存空间,并且每个字节的值都是0。 |
void free(void *address) | 该函数释放 address 所指向的内存块,释放的是动态分配的内存空间。 |
void *malloc(int num) | 在堆区分配一块指定大小的内存空间,用来存放数据。这块内存空间在函数执行完成后不会被初始化,它们的值是未知的。 |
void *realloc(void *address, int newsize) | 该函数重新分配内存,把内存扩展到 newsize。 |
注意:void * 类型表示未确定类型的指针。C、C++ 规定 void * 类型可以通过类型转换强制转换为任何其它类型的指针。
动态分配内存 ¶
编程时,如果您预先知道数组的大小,那么定义数组时就比较容易。例如,一个存储人名的数组,它最多容纳 100 个字符,所以您可以定义数组,如下所示:
c
1char name[100];
但是,如果您预先不知道需要存储的文本长度,例如您向存储有关一个主题的详细描述。在这里,我们需要定义一个指针,该指针指向未定义所需内存大小的字符,后续再根据需求来分配内存,如下所示:
c
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4
5int main()
6{
7 char name[100];
8 char *description;
9
10 strcpy(name, "Zara Ali");
11
12 /* 动态分配内存 */
13 description = (char *)malloc( 200 * sizeof(char) );
14 if( description == NULL )
15 {
16 fprintf(stderr, "Error - unable to allocate required memory\n");
17 }
18 else
19 {
20 strcpy( description, "Zara ali a DPS student in class 10th");
21 }
22 printf("Name = %s\n", name );
23 printf("Description: %s\n", description );
24}
当上面的代码被编译和执行时,它会产生下列结果:
bash
1Name = Zara Ali
2Description: Zara ali a DPS student in class 10th
上面的程序也可以使用 calloc() 来编写,只需要把 malloc 替换为 calloc 即可,如下所示:
c
1calloc(200, sizeof(char));
当动态分配内存时,您有完全控制权,可以传递任何大小的值。而那些预先定义了大小的数组,一旦定义则无法改变大小。
重新调整内存的大小和释放内存 ¶
当程序退出时,操作系统会自动释放所有分配给程序的内存,但是,建议您在不需要内存时,都应该调用函数 free() 来释放内存。
或者,您可以通过调用函数 realloc() 来增加或减少已分配的内存块的大小。让我们使用 realloc() 和 free() 函数,再次查看上面的实例:
c
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4
5int main()
6{
7 char name[100];
8 char *description;
9
10 strcpy(name, "Zara Ali");
11
12 /* 动态分配内存 */
13 description = (char *)malloc( 30 * sizeof(char) );
14 if( description == NULL )
15 {
16 fprintf(stderr, "Error - unable to allocate required memory\n");
17 }
18 else
19 {
20 strcpy( description, "Zara ali a DPS student.");
21 }
22 /* 假设您想要存储更大的描述信息 */
23 description = (char *) realloc( description, 100 * sizeof(char) );
24 if( description == NULL )
25 {
26 fprintf(stderr, "Error - unable to allocate required memory\n");
27 }
28 else
29 {
30 strcat( description, "She is in class 10th");
31 }
32
33 printf("Name = %s\n", name );
34 printf("Description: %s\n", description );
35
36 /* 使用 free() 函数释放内存 */
37 free(description);
38}
当上面的代码被编译和执行时,它会产生下列结果:
c
1Name = Zara Ali
2Description: Zara ali a DPS student.She is in class 10th
您可以尝试一下不重新分配额外的内存,strcat() 函数会生成一个错误,因为存储 description 时可用的内存不足。