JOIN us on
WhatsApp Group Join Now
Telegram Join Join Now

हिंदी माध्यम नोट्स

Categories: C Language in hindi

Dynamic Memory Allocation : Calloc ,Relloc और Free in hindi , कैलोक , रिलोक और फ्री क्या है c कंप्यूटर भाषा में

कैलोक , रिलोक और फ्री क्या है c कंप्यूटर भाषा में , Dynamic Memory Allocation : Calloc ,Relloc और Free in hindi  :-
इससे पहले के article मे , हमने malloc function जोकि memory मे specific memory बाइट के block को run time पर allocate करता है |अब इस article मे calloc function,relloc function और free function को पढेगे |
  1. Calloc ()
    calloc () function भी Dynamic Memory Allocation का दूसरा function है जिससे memory block को run time मे allocate करा सकते है |लेकिन ये function deriving data structure के लिय यूसे किया जाता है |
    malloc function specific memory space का पूरा एक block allocate करता है लेकिन calloc मे specific memory को  multiple memory blocks मे allocate किया जाता है |in blocks मे से पहले block का address हमेशा ‘0’ ही assign होता है |इसका syntax होता है :-

pointer _name=(cast type *) calloc(number of block ,block size);

यहा पर :
pointer _name : ये उस pointer का नाम जिसमे allocate memory blocks मे से पहले blocks का address store होता है |
cast type * : इस pointer variable के type को define करता है |
number of block:ये allocate memory block के number को define करता है |
block size : ये single block के size को define करता है |

इसका  उदाहरण है :

a=(int *) calloc(5,sizeof(int));

इस statement से .int size में 5 blocks assign हो जायेगा|सभी block की size integer की size होगी |सभी blocks का address ‘0’ से initial हो जायेगा |और पहले block का address return होगा |अगर space फुल होगा तब null pointer return होगा |

calloc function को struture के साथ use कर सकते है |इसका syntax है :-

pointer _name=(cast type *) calloc(number of structure blocks ,structure size);

यहा पर :
pointer _name : ये उस pointer का नाम जिसमे allocate memory blocks मे से पहले blocks का address store होता है |
cast type * : इस pointer variable के type को define करता है |structure के case मे, structure variable का नाम हो cast type होता है |
number of block:ये allocate structure blocks के number को define करता है |
block size : ये single structure block के size को define करता है |

इसका उदाहरण है :-

struct_variable =(struct student *) calloc(class size ,sizeof(struct student));

इस उदाहरण में, structure student के type का variable struct_variable है |ओए calloc function से structure student के size का number(class size) blocks allocate हो जाते है |

उदाहरण के लिए
#include<conio.h>
#include<stdoio.h>
struct stu
{
char name [20];
int year ;
int salary;
char add[30];
}student;
void main()
{
int *p;d;
int class_size;
printf(“Enter Size of class”);
scanf(“%d”,&class_size);
if(d=(struct student *) calloc(class_size,sizeof(struct student))==NULL)
{
printf(“No more space “);
}
else
{
printf(“Address of first block = %u”,d);
}
for(p=d,i=1;i<class_size;i++,p++ )
{
printf(“Enter data of student “);
scanf(“%s %d %d %s”,p->name,p->year,p->salary,p->add);
}
for(p=d,i=1;i<class_size;i++,p++ )
{
printf(“Detail of student %d”,i);
printf(“Name :%s ,Birth Year :%d  Salary :%d Address:%s\n”,p->name,p->year,p->salary,p->add);
}
free(d);
getch()
}

इस उदाहरण मे ,student का structure variable है |और ‘p’ और ‘t’ दो pointer है |
d=(struct student *) calloc(class_size,sizeof(struct student)) statement से यूजर द्वारा दिए गये class_size की value के अनुसार memory block allocate हो जाते है |प्रत्येक memory block की size single structure student की size के सामान होती है |

2.Free ()
जब किसी compile time variable को allocate किया जाता है तब ये प्रोग्रम के terminate होते ही automated terminate हो जाता है |लेकिन Dynamic Memory Allocation  मे ,allocated space की responsibility प्रोग्रामर पर होती है इसलिए free() का use किया जाता है |
free() function का use, malloc और calloc function से allocate स्पच्वे को डिलीट किया जाता है |इसका syntax है :-
free(Pointer_variable);

इस statement मे ,pointer_variable उस memory space का address को contain करता है जिसे डिलीट करना है |

3.Realloc()
जब allocate space की size छोटी या बड़ी होती है तब इस function का use memory space का size को modify कर सकते है |इस प्रोसेस को reallocation ऑफ़ memory कहते है |इसका syntax है :-

pointer_name = realloc (pointer_name , new size );

इस statement मे , pointer_name एक pointer का नाम होता है जिसे modify करना है |और new size नई allocate memory की size होती है |

realloc() function इस new memory block के first bytes के address को pointer variable मे assign होता है |
कई बार पुराने block मे modification नहीं होता है |अतः इस statement से नया block बनता है पुराने block मे से data नए बोक्क मे transfer हो जाता है |और पुराना block डिलीट हो जाता है |इस फ़ुन्क्त्रिओन से data मे कोई नुकसान नहीं होती है |
अगर function को नए space के लिए जगह नहीं मिलती है तब null pointer return होता है |

उदाहरण के लिए :
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int *p;
if(p=(char *)malloc(10)==NULL)
{
printf(“No more space”);
}
{
strcpy(p,”ParthPatel”);
printf(“Block have = %s”,p);
}
if(p=(Char *)realloc(p,15)==NULL)
{
printf(“No more space”);
}
else
{
printf(“Memory Block Modified”);
strcpy(p,”OmPrakashSaini”);
printf(“Modified Memory Block have=%s”,p);
free(p);
getch();
}

इस उदाहरण मे ,

p=(char *)malloc(10) एक memory block allocate होगा जिसका size 10 bytes है |बाद मे ParthPatel को string मे सेव करा देगे |
बाद मे , p=(Char *)realloc(p,15) से memory block की size को ’10’ से ’15’ मे modified कर देते है |और इसमें OmPrakashSaini को copy करा देते है |


आउटपुट होगा :
Block have = ParthPatel
Modified Memory Block have = OmPrakashSaini

Sbistudy

Recent Posts

मालकाना का युद्ध malkhana ka yudh kab hua tha in hindi

malkhana ka yudh kab hua tha in hindi मालकाना का युद्ध ? मालकाना के युद्ध…

4 weeks ago

कान्हड़देव तथा अलाउद्दीन खिलजी के संबंधों पर प्रकाश डालिए

राणा रतन सिंह चित्तौड़ ( 1302 ई. - 1303 ) राजस्थान के इतिहास में गुहिलवंशी…

4 weeks ago

हम्मीर देव चौहान का इतिहास क्या है ? hammir dev chauhan history in hindi explained

hammir dev chauhan history in hindi explained हम्मीर देव चौहान का इतिहास क्या है ?…

4 weeks ago

तराइन का प्रथम युद्ध कब और किसके बीच हुआ द्वितीय युद्ध Tarain battle in hindi first and second

Tarain battle in hindi first and second तराइन का प्रथम युद्ध कब और किसके बीच…

4 weeks ago

चौहानों की उत्पत्ति कैसे हुई थी ? chahamana dynasty ki utpatti kahan se hui in hindi

chahamana dynasty ki utpatti kahan se hui in hindi चौहानों की उत्पत्ति कैसे हुई थी…

1 month ago

भारत पर पहला तुर्क आक्रमण किसने किया कब हुआ first turk invaders who attacked india in hindi

first turk invaders who attacked india in hindi भारत पर पहला तुर्क आक्रमण किसने किया…

1 month ago
All Rights ReservedView Non-AMP Version
X

Headline

You can control the ways in which we improve and personalize your experience. Please choose whether you wish to allow the following:

Privacy Settings
JOIN us on
WhatsApp Group Join Now
Telegram Join Join Now