Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C

malloc ในภาษา C คืออะไร?


ฟังก์ชันการจัดสรรหน่วยความจำ C ไลบรารีเป็นโมฆะ *malloc(ขนาด size_t) จะจัดสรรหน่วยความจำที่ร้องขอและส่งคืนตัวชี้ไปยังหน่วยความจำนั้น

ฟังก์ชันการจัดสรรหน่วยความจำ

หน่วยความจำสามารถจัดสรรได้สองวิธีตามที่อธิบายไว้ด้านล่าง -

malloc ในภาษา C คืออะไร?

เมื่อจัดสรรหน่วยความจำ ณ เวลาคอมไพล์แล้ว จะไม่สามารถเปลี่ยนแปลงได้ในระหว่างการดำเนินการ จะมีปัญหาเรื่องความไม่เพียงพอหรือการสูญเสียความจำอย่างอื่น

วิธีแก้ไขคือสร้างหน่วยความจำแบบไดนามิก เช่น ตามความต้องการของผู้ใช้ระหว่างการทำงานของโปรแกรม

ฟังก์ชันไลบรารีมาตรฐานที่ใช้สำหรับการจัดการหน่วยความจำแบบไดนามิกมีดังนี้ -

  • malloc ( )
  • calloc ( )
  • realloc ( )
  • ฟรี ( )

ฟังก์ชัน Malloc()

ฟังก์ชันนี้ใช้สำหรับการจัดสรรบล็อกหน่วยความจำเป็นไบต์ที่รันไทม์ ส่งคืนตัวชี้เป็นโมฆะ ซึ่งชี้ไปยังที่อยู่ฐานของหน่วยความจำที่จัดสรร

ไวยากรณ์สำหรับ malloc() มีดังต่อไปนี้ −

void *malloc (size in bytes)

ตัวอย่างที่ 1

ตัวอย่างต่อไปนี้แสดงการใช้งานฟังก์ชัน malloc()

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

หมายเหตุ - คืนค่า NULL หากหน่วยความจำไม่ว่าง

ตัวอย่างโปรแกรม

รับด้านล่างเป็นโปรแกรม C เพื่อแสดงฟังก์ชันการจัดสรรหน่วยความจำแบบไดนามิก - malloc()

#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 เพื่อแสดงองค์ประกอบโดยใช้ฟังก์ชันการจัดสรรหน่วยความจำแบบไดนามิก -

ห้าบล็อกแรกควรว่างเปล่า ห้าบล็อกที่สองควรมีตรรกะ

#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