C動的なメモリ割り当て
配列の宣言されたサイズを変更することはできません後、アレイは、多数の固定値の集合です。時々 、配列のサイズが十分ではないかもしれないが、我々は、動的な拡張を必要としています。この問題を解決するには、手動で実行時にメモリを割り当てることができます。これは、プログラムCに参照される動的なメモリ割り当てを。
動的なメモリ割り当て関数は、ライブラリーに関連しています
-
malloc()
-
calloc()
-
realloc()
-
free()
これらの機能はれる<stdlib.h>
ヘッダファイルに定義されています。
1.malloc()
名前「malloc関数は、」メモリ割り当て、メモリ割り当てを表します。
malloc()
予備メモリブロックの機能は、バイトの数を指定しました。その後、それは返すポインタするvoid
ポインタの任意の形式にキャストすることを。
malloc()の構文
ptr = (castType*) malloc(size);
例
ptr = (int*) malloc(100 * sizeof(float));
上記の文は、メモリの400バイトを割り当てられています。これがためであるfloat
4バイトのサイズ。さらに、PTRポインタメモリに記憶された最初のバイトは、メモリアドレスを割り当て。
あなたはメモリを割り当てることができない場合は、式が生成されますNULL
ポインタを。
2.calloc()
名前「のcallocは、」連続分布、連続割り当てを表します。
malloc()
関数は、メモリを割り当てますが初期化しないメモリを行います。calloc()
関数は、メモリを割り当て、すべてのビットはゼロに初期化。
calloc()構文
ptr = (castType*)calloc(n, size);
例:
ptr = (float*) calloc(25, sizeof(float));
上記のステートメントは、float
メモリに割り当てられた連続空間型の素子25。
3.free()
使用calloc()
またはmalloc()
明示的に使用する必要があり、個別に作成し、動的に割り当てられたメモリを解放しないfree()
空きスペースを。
無料()構文
free(ptr);
スペース割り当てられたメモリが発表文は指摘しましたptr
。
例1:のmalloc()とfree()
// Program to calculate the sum of n numbers entered by the user
#include <stdio.h>
#include <stdlib.h> int main() { int n, i, *ptr, sum = 0; printf("Enter number of elements: "); scanf("%d", &n); ptr = (int*) malloc(n * sizeof(int)); // if memory cannot be allocated if(ptr == NULL) { printf("Error! memory not allocated."); exit(0); } printf("Enter elements: "); for(i = 0; i < n; ++i) { scanf("%d", ptr + i); sum += *(ptr + i); } printf("Sum = %d", sum); // deallocating the memory free(ptr); return 0; }
ここでは、n桁にメモリの動的割り当てを持っています
例2:はcalloc()とfree()
// Program to calculate the sum of n numbers entered by the user
#include <stdio.h>
#include <stdlib.h> int main() { int n, i, *ptr, sum = 0; printf("Enter number of elements: "); scanf("%d", &n); ptr = (int*) calloc(n, sizeof(int)); if(ptr == NULL) { printf("Error! memory not allocated."); exit(0); } printf("Enter elements: "); for(i = 0; i < n; ++i) { scanf("%d", ptr + i); sum += *(ptr + i); } printf("Sum = %d", sum); free(ptr); return 0; }
4.realloc()
メモリ不足の動的割り当てた場合や要件を超えて、あなたはこれを使用することができrealloc()
、以前に割り当てられたメモリのサイズを変更する機能を。
realloc()構文
ptr = realloc(ptr, x);
ここでは、ptrが新しいサイズxに再割り当て。
実施例3:のrealloc()
#include <stdio.h>
#include <stdlib.h> int main() { int *ptr, i , n1, n2; printf("Enter size: "); scanf("%d", &n1); ptr = (int*) malloc(n1 * sizeof(int)); printf("Addresses of previously allocated memory: "); for(i = 0; i < n1; ++i) printf("%u\n",ptr + i); printf("\nEnter the new size: "); scanf("%d", &n2); // rellocating the memory ptr = realloc(ptr, n2 * sizeof(int)); printf("Addresses of newly allocated memory: "); for(i = 0; i < n2; ++i) printf("%u\n", ptr + i); free(ptr); return 0; }
あなたがプログラムを実行すると、出力は次のようになります。
输入大小:2
先前分配的内存的地址:26855472
26855476
输入新的尺寸:4
新分配的内存地址:26855472
26855476
26855480
26855484
概要
malloc関数の動的メモリ割り当て、初期化しません
int n, *ptr = 0;
printf("Enter number of elements: ");
scanf("%d", &n); ptr = (int*) malloc(n * sizeof(int));
あなたはメモリを割り当てることができない場合、NULLが返されます
callocの動的メモリ割り当て、全ビットの初期化0
int n, *ptr = 0;
printf("Enter number of elements: ");
scanf("%d", &n); ptr = (int*) calloc(n, sizeof(int));
あなたはメモリを割り当てることができない場合、NULLが返されます
リリース・メモリ
free(ptr);
reallocのREALLOCATEメモリ
ptr = realloc(ptr, n * sizeof(int));