使用malloc()、calloc()、free()和realloc()在C中进行动态内存分配

由于C是一种结构化语言, 因此它具有一些固定的编程规则。其中之一包括更改数组的大小。数组是存储在连续内存位置的项目的集合。
使用malloc()、calloc()、free()和realloc()在C中进行动态内存分配_第1张图片

可以看出, 上述数组的长度(大小)为9。但是, 如果需要更改此长度(大小), 该怎么办。例如,

如果存在只需要在此数组中输入5个元素的情况。在这种情况下, 剩余的4个索引只会浪费该数组中的内存。因此需要将数组的长度(大小)从9减少到5。

采取另一种情况。在这里, 有9个元素组成的数组, 所有9个索引均已填充。但是需要在此数组中再输入3个元素。在这种情况下, 还需要3个索引。因此, 阵列的长度(大小)需要从9更改为12。

此过程称为C中的动态内存分配.

因此, C动态内存分配可以定义为在运行时更改数据结构(如Array)的大小的过程。

C提供了一些功能来完成这些任务。 C下定义了4个提供的库函数头文件, 以方便C编程中的动态内存分配。他们是:

  1. malloc()
  2. calloc()
  3. 自由()
  4. realloc()

让我们更详细地研究它们。

C malloc()方法

" malloc"or"内存分配"C语言中的方法用于动态分配具有指定大小的单个大内存块。它返回void类型的指针, 该指针可以转换为任何形式的指针。它使用默认垃圾值初始化每个块。

语法如下:

ptr = (cast-type*) malloc(byte-size)

例如:

ptr =(int )malloc(100 sizeof(int));由于int的大小为4个字节, 因此此语句将分配400个字节的内存。并且, 指针ptr保存分配的存储器中的第一个字节的地址。

使用malloc()、calloc()、free()和realloc()在C中进行动态内存分配_第2张图片

如果空间不足, 分配将失败并返回NULL指针。

例子:

#include 
#include 
  
int main()
{
  
     // This pointer will hold the
     // base address of the block created
     int * ptr;
     int n, i;
  
     // Get the number of elements for the array
     n = 5;
     printf ( "Enter number of elements: %dn" , n);
  
     // Dynamically allocate memory using malloc()
     ptr = ( int *) malloc (n * sizeof ( int ));
  
     // Check if the memory has been successfully
     // allocated by malloc or not
     if (ptr == NULL) {
         printf ( "Memory not allocated.n" );
         exit (0);
     }
     else {
  
         // Memory has been successfully allocated
         printf ( "Memory successfully allocated using malloc.n" );
  
         // Get the elements of the array
         for (i = 0; i < n; ++i) {
             ptr[i] = i + 1;
         }
  
         // Print the elements of the array
         printf ( "The elements of the array are: " );
         for (i = 0; i < n; ++i) {
             printf ( "%d, " , ptr[i]);
         }
     }
  
     return 0;
}

输出如下:

Enter number of elements: 5
Memory successfully allocated using malloc.
The elements of the array are: 1, 2, 3, 4, 5, 

C calloc()方法

" calloc"or"连续分配"C语言中的方法用于动态分配指定数量的指定类型的内存块。它使用默认值" 0"初始化每个块。

语法如下:

ptr = (cast-type*)calloc(n, element-size);

例如:

ptr =(float *)calloc(25, sizeof(float));该语句在内存中为25个元素分配连续的空间, 每个元素的大小为float。

使用malloc()、calloc()、free()和realloc()在C中进行动态内存分配_第3张图片

如果空间不足, 分配将失败并返回NULL指针。

例子:

#include 
#include 
  
int main()
{
  
     // This pointer will hold the
     // base address of the block created
     int * ptr;
     int n, i;
  
     // Get the number of elements for the array
     n = 5;
     printf ( "Enter number of elements: %dn" , n);
  
     // Dynamically allocate memory using calloc()
     ptr = ( int *) calloc (n, sizeof ( int ));
  
     // Check if the memory has been successfully
     // allocated by calloc or not
     if (ptr == NULL) {
         printf ( "Memory not allocated.n" );
         exit (0);
     }
     else {
  
         // Memory has been successfully allocated
         printf ( "Memory successfully allocated using calloc.n" );
  
         // Get the elements of the array
         for (i = 0; i < n; ++i) {
             ptr[i] = i + 1;
         }
  
         // Print the elements of the array
         printf ( "The elements of the array are: " );
         for (i = 0; i < n; ++i) {
             printf ( "%d, " , ptr[i]);
         }
     }
  
     return 0;
}

输出如下:

Enter number of elements: 5
Memory successfully allocated using calloc.
The elements of the array are: 1, 2, 3, 4, 5, 

C free()方法

"free"C中的方法用于动态取消分配内存。使用函数malloc()和calloc()分配的内存不会自行取消分配。因此, 每当发生动态内存分配时, 都会使用free()方法。它通过释放内存来帮助减少内存浪费。

语法如下:

free(ptr);

使用malloc()、calloc()、free()和realloc()在C中进行动态内存分配_第4张图片

例子:

#include 
#include 
  
int main()
{
  
     // This pointer will hold the
     // base address of the block created
     int *ptr, *ptr1;
     int n, i;
  
     // Get the number of elements for the array
     n = 5;
     printf ( "Enter number of elements: %dn" , n);
  
     // Dynamically allocate memory using malloc()
     ptr = ( int *) malloc (n * sizeof ( int ));
  
     // Dynamically allocate memory using calloc()
     ptr1 = ( int *) calloc (n, sizeof ( int ));
  
     // Check if the memory has been successfully
     // allocated by malloc or not
     if (ptr == NULL || ptr1 == NULL) {
         printf ( "Memory not allocated.n" );
         exit (0);
     }
     else {
  
         // Memory has been successfully allocated
         printf ( "Memory successfully allocated using malloc.n" );
  
         // Free the memory
         free (ptr);
         printf ( "Malloc Memory successfully freed.n" );
  
         // Memory has been successfully allocated
         printf ( "nMemory successfully allocated using calloc.n" );
  
         // Free the memory
         free (ptr1);
         printf ( "Calloc Memory successfully freed.n" );
     }
  
     return 0;
}

输出如下:

Enter number of elements: 5
Memory successfully allocated using malloc.
Malloc Memory successfully freed.

Memory successfully allocated using calloc.
Calloc Memory successfully freed.

C realloc()方法

"重新分配"or"重新分配"C中的方法用于动态更改先前分配的内存的内存分配。换句话说, 如果先前借助malloc或calloc分配的内存不足, 则可以使用realloc来动态重新分配内存。内存的重新分配将保持已经存在的值, 并且新块将使用默认垃圾值进行初始化。

语法如下:

ptr = realloc(ptr, newSize);

where ptr is reallocated with new size 'newSize'.

使用malloc()、calloc()、free()和realloc()在C中进行动态内存分配_第5张图片

如果空间不足, 分配将失败并返回NULL指针。

例子:

#include 
#include 
  
int main()
{
  
     // This pointer will hold the
     // base address of the block created
     int * ptr;
     int n, i;
  
     // Get the number of elements for the array
     n = 5;
     printf ( "Enter number of elements: %dn" , n);
  
     // Dynamically allocate memory using calloc()
     ptr = ( int *) calloc (n, sizeof ( int ));
  
     // Check if the memory has been successfully
     // allocated by malloc or not
     if (ptr == NULL) {
         printf ( "Memory not allocated.n" );
         exit (0);
     }
     else {
  
         // Memory has been successfully allocated
         printf ( "Memory successfully allocated using calloc.n" );
  
         // Get the elements of the array
         for (i = 0; i < n; ++i) {
             ptr[i] = i + 1;
         }
  
         // Print the elements of the array
         printf ( "The elements of the array are: " );
         for (i = 0; i < n; ++i) {
             printf ( "%d, " , ptr[i]);
         }
  
         // Get the new size for the array
         n = 10;
         printf ( "nnEnter the new size of the array: %dn" , n);
  
         // Dynamically re-allocate memory using realloc()
         ptr = realloc (ptr, n * sizeof ( int ));
  
         // Memory has been successfully allocated
         printf ( "Memory successfully re-allocated using realloc.n" );
  
         // Get the new elements of the array
         for (i = 5; i < n; ++i) {
             ptr[i] = i + 1;
         }
  
         // Print the elements of the array
         printf ( "The elements of the array are: " );
         for (i = 0; i < n; ++i) {
             printf ( "%d, " , ptr[i]);
         }
  
         free (ptr);
     }
  
     return 0;
}

输出如下:

Enter number of elements: 5
Memory successfully allocated using calloc.
The elements of the array are: 1, 2, 3, 4, 5, Enter the new size of the array: 10
Memory successfully re-allocated using realloc.
The elements of the array are: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 

更多C/C++开发相关内容请参考:lsbin - IT开发技术https://www.lsbin.com/

查看以下更多C语言相关的内容:

你可能感兴趣的:(使用malloc()、calloc()、free()和realloc()在C中进行动态内存分配)