logo

Calloc in C

In dit onderwerp wordt besproken hoe u dynamische geheugentoewijzing kunt creëren met behulp van de calloc()-functie in de programmeertaal C. Voordat we de concepten doornemen, bespreken we eerst de dynamische geheugentoewijzing in C. Dynamisch geheugen is een procedure voor structuurprogrammering waarmee gebruikers het geheugen kunnen toewijzen tijdens de looptijd van een programma. Met behulp van dynamische geheugentoewijzing kunnen we het geheugen vergroten of verkleinen tijdens de uitvoering van een programma. Op deze manier vermijdt het de verspilling van computergeheugen. Een geheugentoewijzing is verdeeld in twee delen: malloc() en calloc() functie.

Calloc in C

A calloc()-functie is een vooraf gedefinieerde bibliotheekfunctie die staat voor aaneengesloten geheugentoewijzing . Een calloc()-functie wordt gebruikt om meerdere blokken te creëren tijdens de looptijd van een programma met dezelfde grootte in het geheugen. Er wordt een calloc-functie gedefinieerd binnen de stdlib.h header-bestand. Het heeft twee parameters, nee. aantal blokken en de grootte van elk blok. Wanneer het dynamische geheugen wordt toegewezen met behulp van de calloc()-functie, retourneert het het basisadres van het eerste blok, en wordt elk blok geïnitialiseerd met 0. En als er geen geheugen wordt aangemaakt, retourneert het een NULL-aanwijzer.

Stel dat we bijvoorbeeld drie geheugenblokken willen maken met behulp van de calloc()-functie, dan moeten we twee parameters, een aantal blokken (3) en de grootte van elk blok (int, char, float, etc.) doorgeven de byte. Op deze manier creëert het drie blokken waarvan de grootte hetzelfde is in het computergeheugen.

Syntaxis

 ptr = (cast_type *) calloc ( number_of_blocks, size_of_block); 

In de bovenstaande syntaxis heeft de functie calloc() twee parameters. De eerste parameter definieert de aantal blokken en de tweede parameter definieert de grootte van elk blok in het geheugen. De grootte van de blokken en cast_type kan in int, char, float, etc. zijn.

Opbrengst : Het retourneert het basisadres van het eerste blok naar de ptr-variabele.

Programma om te controleren of dynamisch geheugen is toegewezen met behulp van de calloc()-functie

Laten we een eenvoudig programma schrijven om te controleren of het dynamische geheugen is toegewezen in C.

programma.c

 #include #include int main() { int *ptr; /* use calloc() function to define the no. of blocks and size of each blocks. */ ptr = calloc (4, sizeof(int)); // here 4 is the no. of block and int is the size of block if (ptr != NULL) { printf (' Memory is created successfully 
'); } else printf (' Memory is not created '); return 0; } 

Uitgang:

 Memory is created successfully 

Programma om het gebruik van de calloc()-functie te demonstreren

Laten we eens kijken naar het creëren van dynamische geheugentoewijzing met behulp van de calloc()-functie en het opslaan van gegevens in de geheugenblokken.

Programma2.c

 #include #include #include void main() { int n, *ptr, *p, i, sum = 0; /* n = number of elements, *ptr = store base address of the dynamic memory, *p store temporary address of the *ptr */ printf (' Enter the number of elements: '); scanf (' %d', &n); // it takes number of elements // use calloc syntax to create memory block of int data type ptr = (int *) calloc (n, sizeof(int)); p = ptr; // assign the address of ptr if (ptr == NULL) // it checks whether the memory is allocated { printf (' Memory is not allocated. '); exit(0); // exit from the program } printf (' Enter %d numbers 
&apos;, n); for ( i = 1; i <= n; i++) { scanf ( '%d', ptr); sum="sum" + *ptr; ptr++; } printf (' elements are: '); for (i="1;" i <="n;" %d', *p); p++; 
 the addition of is: %d ', sum); getch(); pre> <p> <strong>Output:</strong> </p> <pre> Enter the number of elements: 5 Enter 5 numbers 1 2 3 4 5 Elements are: 1 2 3 4 5 The addition of the elements is: 15 </pre> <h3>Program to release dynamic memory allocation using free() function</h3> <p> <strong>free() function:</strong> A free() function is used to release the dynamic memory which is created either <strong>calloc</strong> () or <strong>malloc</strong> () function. These allocated memories cannot be freed to their own, and they will exist till the end of the program. So, it is our responsibility to release that memory that can be reused, and hence we explicitly use the free() function to release the memory.</p> <p> <strong>Syntax</strong> </p> <pre> free (ptr); </pre> <p>Here free() is a function that releases the allocated memory using the pointer ptr variable.</p> <p>Let&apos;s consider creating dynamic memory allocation using the calloc() function and then releasing occupied space using the free() function in the C program.</p> <p> <strong>Release.c</strong> </p> <pre> #include #include #include void main() { int n, *ptr, *p, i, sum = 0; printf (&apos; Define the number of elements to be entered: &apos;); scanf (&apos; %d&apos;, &amp;n); // use calloc syntax to create memory block of int data type ptr = (int *) calloc (n, sizeof(int)); p = ptr; // store the base address in p if (ptr == NULL) { printf (&apos; Out of memory &apos;); exit(0); } printf (&apos; Enter the elements 
&apos;, n); for ( i = 1; i <= n; i++) { scanf ( '%d', ptr); sum="sum" + *ptr; ptr++; } printf (' elements are: '); for (i="1;" i <="n;" %d', *p); p++; 
 the addition of is: %d ', sum); free(ptr); * use free() function to release dynamic memory allocation getch(); pre> <p> <strong>Output:</strong> </p> <pre> Define the number of elements to be entered: 6 Enter the elements 2 4 6 8 10 12 Elements are: 2 4 6 8 10 12 The addition of the elements is: 42 </pre> <hr></=></pre></=>

Programma om dynamische geheugentoewijzing vrij te geven met behulp van de functie free().

gratis() functie: Een free()-functie wordt gebruikt om het dynamische geheugen dat wordt aangemaakt vrij te geven calloc () of malloc () functie. Deze toegewezen herinneringen kunnen niet voor zichzelf worden vrijgegeven en zullen tot het einde van het programma blijven bestaan. Het is dus onze verantwoordelijkheid om dat geheugen vrij te geven dat kan worden hergebruikt, en daarom gebruiken we expliciet de functie free() om het geheugen vrij te geven.

Syntaxis

 free (ptr); 

Hier is free() een functie die het toegewezen geheugen vrijgeeft met behulp van de pointer ptr-variabele.

Laten we overwegen om dynamische geheugentoewijzing te creëren met behulp van de calloc()-functie en vervolgens bezette ruimte vrij te geven met behulp van de free()-functie in het C-programma.

Release.c

 #include #include #include void main() { int n, *ptr, *p, i, sum = 0; printf (&apos; Define the number of elements to be entered: &apos;); scanf (&apos; %d&apos;, &amp;n); // use calloc syntax to create memory block of int data type ptr = (int *) calloc (n, sizeof(int)); p = ptr; // store the base address in p if (ptr == NULL) { printf (&apos; Out of memory &apos;); exit(0); } printf (&apos; Enter the elements 
&apos;, n); for ( i = 1; i <= n; i++) { scanf ( \'%d\', ptr); sum="sum" + *ptr; ptr++; } printf (\' elements are: \'); for (i="1;" i <="n;" %d\', *p); p++; 
 the addition of is: %d \', sum); free(ptr); * use free() function to release dynamic memory allocation getch(); pre> <p> <strong>Output:</strong> </p> <pre> Define the number of elements to be entered: 6 Enter the elements 2 4 6 8 10 12 Elements are: 2 4 6 8 10 12 The addition of the elements is: 42 </pre> <hr></=>