logo

Calloc u C

U ovoj će se temi raspravljati o tome kako stvoriti dinamičku dodjelu memorije pomoću funkcije calloc() u programskom jeziku C. Prije nego prođemo kroz koncepte, razgovarajmo o dinamičkoj dodjeli memorije u C-u. Dinamička memorija je procedura strukturnog programiranja koja korisnicima omogućuje dodjelu memorije tijekom izvođenja programa. Pomoću dinamičke dodjele memorije možemo povećati ili smanjiti memoriju tijekom izvođenja programa. Na taj način izbjegava rasipanje računalne memorije. Dodjela memorije podijeljena je u dva dijela, a to su funkcija malloc() i calloc().

Calloc u C

A funkcija calloc(). je unaprijed definirana funkcija knjižnice koja označava kontinuirana dodjela memorije . Funkcija calloc() koristi se za stvaranje višestrukih blokova u vremenu izvođenja programa koji ima istu veličinu u memoriji. Funkcija calloc definirana je unutar stdlib.h datoteka zaglavlja. Ima dva parametra, br. blokova i veličinu svakog bloka. Kada se dinamička memorija dodjeljuje pomoću funkcije calloc(), ona vraća osnovnu adresu prvog bloka, a svaki blok se inicijalizira s 0. A ako memorija nije stvorena, vraća NULL pokazivač.

Na primjer, pretpostavimo da želimo stvoriti tri bloka memorije pomoću funkcije calloc(), moramo proslijediti dva parametra, broj blokova (3) i veličinu svakog bloka (int, char, float, itd.) u bajt. Na taj način unutar memorije računala stvara tri bloka iste veličine.

Sintaksa

javac nije prepoznat
 ptr = (cast_type *) calloc ( number_of_blocks, size_of_block); 

U gornjoj sintaksi funkcija calloc() ima dva parametra. Prvi parametar definira broj blokova a drugi parametar definira veličina svakog bloka u sjećanju. Veličina blokova i cast_type mogu biti int, char, float itd.

Povratak : Vraća osnovnu adresu prvog bloka u ptr varijablu.

Program za provjeru dinamičke memorije dodjeljuje se pomoću funkcije calloc().

Napišimo jednostavan program za provjeru je li dinamička memorija dodijeljena u C-u.

program.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; } 

Izlaz:

 Memory is created successfully 

Program za demonstraciju upotrebe funkcije calloc().

Razmotrimo stvaranje dinamičke dodjele memorije pomoću funkcije calloc() i pohranjivanje podataka u memorijske blokove.

Program2.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></=>

Program za oslobađanje dinamičke dodjele memorije pomoću funkcije free().

funkcija free(): Funkcija free() koristi se za oslobađanje dinamičke memorije koja se stvara calloc () ili malloc () funkcija. Ove dodijeljene memorije ne mogu se osloboditi same i postojat će do kraja programa. Dakle, naša je odgovornost osloboditi tu memoriju koja se može ponovno upotrijebiti, pa stoga eksplicitno koristimo funkciju free() za oslobađanje memorije.

c program za usporedbu nizova

Sintaksa

 free (ptr); 

Ovdje je free() funkcija koja oslobađa dodijeljenu memoriju pomoću ptr varijable pokazivača.

Razmotrimo stvaranje dinamičke dodjele memorije pomoću funkcije calloc(), a zatim oslobađanje zauzetog prostora pomoću funkcije free() u C programu.

Otpuštanje.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></=>