因为C是一种结构化语言, 所以它具备一些固定的编程规则。其中之一包括更改数组的大小。数组是存储在连续内存位置的项目的集合。html
能够看出, 上述数组的长度(大小)为9。可是, 若是须要更改此长度(大小), 该怎么办。例如,编程
若是存在只须要在此数组中输入5个元素的状况。在这种状况下, 剩余的4个索引只会浪费该数组中的内存。所以须要将数组的长度(大小)从9减小到5。数组
采起另外一种状况。在这里, 有9个元素组成的数组, 全部9个索引均已填充。可是须要在此数组中再输入3个元素。在这种状况下, 还须要3个索引。所以, 阵列的长度(大小)须要从9更改成12。数据结构
此过程称为C中的动态内存分配.多线程
所以, C动态内存分配能够定义为在运行时更改数据结构(如Array)的大小的过程。函数
C提供了一些功能来完成这些任务。 C下定义了4个提供的库函数<stdlib.h>头文件, 以方便C编程中的动态内存分配。他们是:spa
让咱们更详细地研究它们。线程
" malloc"or"内存分配"C语言中的方法用于动态分配具备指定大小的单个大内存块。它返回void类型的指针, 该指针能够转换为任何形式的指针。它使用默认垃圾值初始化每一个块。指针
语法以下:code
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,
" 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,
"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.
"从新分配"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语言相关的内容: