Saturday, September 1, 2018

C - メモリ管理

この章では、Cの動的メモリ管理について説明します。Cプログラミング言語は、メモリの割り当てと管理のためのいくつかの機能を提供します。 これらの関数は、<stdlib.h>ヘッダーファイルにあります。
Sr.No.Function & Description
1
void *calloc(int num, int size);
This function allocates an array of num elements each of which size in bytes will be size.
2
void free(void *address);
This function releases a block of memory block specified by address.
3
void *malloc(int num);
This function allocates an array of num bytes and leave them uninitialized.
4
void *realloc(void *address, int newsize);
This function re-allocates memory extending it upto newsize.

動的にメモリを割り当てる
プログラミング時に、配列のサイズを認識していれば、それは簡単で配列として定義できます。 たとえば、任意の人物の名前を格納するには、最大100文字にすることができるので、次のように定義することができます

char name[100];
しかし、ここでは、トピックの詳細な説明を保存するなど、保存する必要があるテキストの長さについてわからない状況について検討してみましょう。 ここでは、必要なメモリの量を定義せずに文字へのポインタを定義する必要があります。要件に基づいて、次の例に示すようにメモリを割り当てることができます

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {

   char name[100];
   char *description;

   strcpy(name, "Zara Ali");

   /* allocate memory dynamically */
   description = malloc( 200 * sizeof(char) );
 
   if( description == NULL ) {
      fprintf(stderr, "Error - unable to allocate required memory\n");
   } else {
      strcpy( description, "Zara ali a DPS student in class 10th");
   }
   
   printf("Name = %s\n", name );
   printf("Description: %s\n", description );
}
上記のコードをコンパイルして実行すると、次の結果が生成されます。

Name = Zara Ali
Description: Zara ali a DPS student in class 10th
calloc()を使って同じプログラムを書くことができます。 あなたはmallocを以下のようにcallocに置き換える必要があります
calloc(200, sizeof(char));
したがって、完全に制御でき、サイズを定義した後は変更できない配列とは異なり、メモリを割り当てている間は任意のサイズの値を渡すことができます。


メモリのサイズ変更と解放
あなたのプログラムが出てくると、オペレーティングシステムはあなたのプログラムによって割り当てられたすべてのメモリを自動的に解放しますが、メモリを必要としないときの良い習慣として、free()関数を呼び出すことによってそのメモリを解放する必要があります。

あるいは、関数realloc()を呼び出すことによって、割り当てられたメモリブロックのサイズを増減することができます。 上記のプログラムをもう一度見て、realloc()とfree()関数を使ってみましょう

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {

   char name[100];
   char *description;

   strcpy(name, "Zara Ali");

   /* allocate memory dynamically */
   description = malloc( 30 * sizeof(char) );
 
   if( description == NULL ) {
      fprintf(stderr, "Error - unable to allocate required memory\n");
   } else {
      strcpy( description, "Zara ali a DPS student.");
   }
 
   /* suppose you want to store bigger description */
   description = realloc( description, 100 * sizeof(char) );
 
   if( description == NULL ) {
      fprintf(stderr, "Error - unable to allocate required memory\n");
   } else {
      strcat( description, "She is in class 10th");
   }
   
   printf("Name = %s\n", name );
   printf("Description: %s\n", description );

   /* release memory using free() function */
   free(description);
}

上記のコードをコンパイルして実行すると、次の結果が生成されます。

Name = Zara Ali
Description: Zara ali a DPS student.She is in class 10th
余分なメモリを再割り当てせずに上記の例を試すと、strcat()関数は説明に利用可能なメモリがないためにエラーを返します。





No comments:

Post a Comment

Interfaces and their protocol stacks

インタフェースとそのプロトコルスタック 主要なネットワーク要素に精通した後、これら要素間のインタフェースをよりよく知る時期が来ています。 インタフェースは、MME、SGWおよびPGWが他のネットワーク要素(例えば、HSSまたはPCRF)と協働することを可能にする。 それらの...