乐趣区

关于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
退出移动版