C 動的メモリ割り当て
C 動的メモリ割り当て
このチュートリアルでは、標準ライブラリ関数 malloc()、calloc()、free()、および realloc() を使用して、C プログラムでメモリを動的に割り当てる方法を学習します。
ご存じのとおり、配列は一定数の値のコレクションです。配列のサイズが宣言されると、それを変更することはできません。
宣言した配列のサイズが不十分な場合があります。この問題を解決するには、実行時にメモリを手動で割り当てることができます。これは、C プログラミングでは動的メモリ割り当てとして知られています。
メモリを動的に割り当てるために、ライブラリ関数は malloc()
です 、 calloc()
、 realloc()
と free()
使用されています。これらの関数は <stdlib.h>
で定義されています ヘッダー ファイル。
C malloc()
「malloc」という名前は、メモリ割り当てを表します。
malloc()
関数は、指定されたバイト数のメモリ ブロックを予約します。そして、void
のポインターを返します。 これは、任意の形式のポインターにキャストできます。
malloc() の構文
ptr = (castType*) malloc(size);
例
ptr = (float*) malloc(100 * sizeof(float));
上記のステートメントは、400 バイトのメモリを割り当てます。 float
のサイズだからです。 4バイトです。そして、ポインター ptr 割り当てられたメモリの最初のバイトのアドレスを保持します。
式は NULL
になります メモリを割り当てることができない場合はポインタ
C calloc()
「calloc」という名前は、連続した割り当てを表します。
malloc()
関数はメモリを割り当て、メモリを初期化しないままにしますが、 calloc()
関数はメモリを割り当て、すべてのビットをゼロに初期化します。
calloc() の構文
ptr = (castType*)calloc(n, size);
例:
ptr = (float*) calloc(25, sizeof(float));
上記のステートメントは、float
型の 25 個の要素に対してメモリ内の連続した領域を割り当てます。 .
C free()
calloc()
のいずれかで作成された動的に割り当てられたメモリ または malloc()
自分では解放されません。 free()
を明示的に使用する必要があります スペースを解放します。
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;
}
出力
Enter number of elements: 3
Enter elements: 100
20
36
Sum = 156
ここでは、n のメモリを動的に割り当てています。 int
の数 .
例 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;
}
出力
Enter number of elements: 3
Enter elements: 100
20
36
Sum = 156
C 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:\n");
for(i = 0; i < n1; ++i)
printf("%pc\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:\n");
for(i = 0; i < n2; ++i)
printf("%pc\n", ptr + i);
free(ptr);
return 0;
}
出力
Enter size: 2 Addresses of previously allocated memory: 26855472 26855476 Enter the new size: 4 Addresses of newly allocated memory: 26855472 26855476 26855480 26855484
C言語