关于c:使用malloccallocfree和realloc在C中进行动态内存分配

因为C是一种结构化语言, 因而它具备一些固定的编程规定。其中之一包含更改数组的大小。数组是存储在间断内存地位的我的项目的汇合。

能够看出, 上述数组的长度(大小)为9。然而, 如果须要更改此长度(大小), 该怎么办。例如,

如果存在只须要在此数组中输出5个元素的状况。在这种状况下, 残余的4个索引只会节约该数组中的内存。因而须要将数组的长度(大小)从9缩小到5。

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

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

因而, C动态内存调配能够定义为在运行时更改数据结构(如Array)的大小的过程。

C提供了一些性能来实现这些工作。 C下定义了4个提供的库函数<stdlib.h>头文件, 以不便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保留调配的存储器中的第一个字节的地址。

如果空间有余, 调配将失败并返回NULL指针。

例子:

#include <stdio.h>
#include <stdlib.h>
  
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。

如果空间有余, 调配将失败并返回NULL指针。

例子:

#include <stdio.h>
#include <stdlib.h>
  
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);

例子:

#include <stdio.h>
#include <stdlib.h>
  
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'.

如果空间有余, 调配将失败并返回NULL指针。

例子:

#include <stdio.h>
#include <stdlib.h>
  
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语言相干的内容:

  • C语言中的exec函数家族介绍和用法:https://www.lsbin.com/3438.html
  • C语言如何了解和应用多线程?:https://www.lsbin.com/3402.html
  • C语言如何从一个函数返回多个值?:https://www.lsbin.com/1809.html

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理