Cプログラミング
 Computer >> コンピューター >  >> プログラミング >> Cプログラミング

C言語のmallocとは何ですか?


Cライブラリのメモリ割り当て関数void*malloc(size_t size)は、要求されたメモリを割り当て、そのメモリへのポインタを返します。

メモリ割り当て関数

以下に説明するように、メモリは2つの方法で割り当てることができます-

C言語のmallocとは何ですか?

コンパイル時にメモリが割り当てられると、実行中にメモリを変更することはできません。不十分であるか、そうでなければメモリの浪費の問題があります。

解決策は、動的に、つまりプログラムの実行中のユーザーの要件に従ってメモリを作成することです。

動的メモリ管理に使用される標準ライブラリ関数は次のとおりです-

  • malloc()
  • calloc()
  • realloc()
  • 無料()

Malloc()関数

この関数は、実行時にメモリのブロックをバイト単位で割り当てるために使用されます。割り当てられたメモリのベースアドレスを指すvoidポインタを返します。

malloc()の構文は次のとおりです-

void *malloc (size in bytes)

例1

次の例は、malloc()関数の使用法を示しています。

int *ptr;
ptr = (int * ) malloc (1000);
int *ptr;
ptr = (int * ) malloc (n * sizeof (int));

注-メモリが空いていない場合はNULLを返します。

サンプルプログラム

以下に示すのは、動的メモリ割り当て関数であるmalloc()を示すCプログラムです。

#include<stdio.h>
#include<stdlib.h>
void main(){
   //Declaring variables and pointer//
   int numofele,i;
   int *p;
   //Reading elements as I/p//
   printf("Enter the number of elements in the array: ");
   scanf("%d",&numofele);
   //Declaring malloc function//
   p = (int *)malloc(numofele * (sizeof(int)));
   //Reading elements into array of pointers//
   for(i=0;i<numofele;i++){
      p[i]=i+1;
      printf("Element %d of array is : %d\n",i,p[i]);
   }
}

出力

上記のプログラムを実行すると、次の結果が得られます-

Enter the number of elements in the array: 4
Element 0 of array is : 1
Element 1 of array is : 2
Element 2 of array is : 3
Element 3 of array is : 4

例2

以下は、動的メモリ割り当て関数を使用して要素を表示するCプログラムです-

最初の5つのブロックは空で、次の5つのブロックにはロジックが必要です。

#include<stdio.h>
#include<stdlib.h>
void main(){
   //Declaring variables and pointers,sum//
   int numofe,i,sum=0;
   int *p;
   //Reading number of elements from user//
   printf("Enter the number of elements : ");
   scanf("%d",&numofe);
   //Calling malloc() function//
   p=(int *)malloc(numofe*sizeof(int));
   /*Printing O/p -
   We have to use if statement because we have to check if memory
   has been successfully allocated/reserved or not*/
   if (p==NULL){
      printf("Memory not available");
      exit(0);
   }
   //Printing elements//
   printf("Enter the elements : \n");
   for(i=0;i<numofe;i++){
      scanf("%d",p+i);
      sum=sum+*(p+i);
   }
   printf("\nThe sum of elements is %d",sum);
   free(p);//Erase first 2 memory locations//
   printf("\nDisplaying the cleared out memory location : \n");
   for(i=0;i<numofe;i++){
      printf("%d\n",p[i]);//Garbage values will be displayed//
   }
}

出力

上記のプログラムを実行すると、次の結果が得られます-

Enter the number of elements : 5
Enter the elements :
12
10
24
45
67
The sum of elements is 158
Displaying the cleared out memory location :
7804032
0
7799120
0
67

  1. C#プログラミングとは何ですか?

    C#は、Microsoftによって開発された最新の汎用オブジェクト指向プログラミング言語です。 C#は、共通言語インフラストラクチャ(CLI)用に設計されています。これは、実行可能コードとランタイム環境で構成されており、さまざまなコンピュータープラットフォームやアーキテクチャでさまざまな高級言語を使用できます。 C#の機能は次のとおりです- ブール条件 自動ガベージコレクション 標準ライブラリ アセンブリのバージョン管理 プロパティとイベント デリゲートとイベント管理 使いやすいジェネリック インデクサー 条件付きコンパイル シンプルなマルチスレッド LINQおよびLambda式 Win

  2. C#の辞書とは何ですか?

    辞書は、C#のキーと値のコレクションです。辞書はSystem.Collection.Generics名前空間に含まれています。 辞書を宣言して初期化するには- IDictionary<int, int> d = new Dictionary<int, int>(); 上記では、キーと値のタイプは、ディクショナリオブジェクトを宣言するときに設定されます。 intはキーの一種であり、stringは値の一種です。どちらもdという名前の辞書オブジェクトに保存されます。 例を見てみましょう- 例 using System; using System.Collections.