logo

Calloc C

Šioje temoje bus aptarta, kaip sukurti dinaminį atminties paskirstymą naudojant funkciją calloc() C programavimo kalba. Prieš peržvelgdami sąvokas, aptarkime dinaminį atminties paskirstymą C. Dinaminė atmintis yra struktūros programavimo procedūra, leidžianti vartotojams paskirstyti atmintį programos vykdymo metu. Naudodami dinaminį atminties paskirstymą, programos vykdymo metu galime padidinti arba sumažinti atmintį. Tokiu būdu išvengiama kompiuterio atminties švaistymo. Atminties paskirstymas yra padalintas į dvi dalis: malloc() ir calloc() funkcijas.

Calloc C

A calloc() funkcija yra iš anksto nustatyta bibliotekos funkcija, kuri reiškia gretimos atminties paskirstymas . Funkcija calloc () naudojama sukurti kelis blokus, kai vykdoma programa, kurios atmintyje yra tokio pat dydžio. Calloc funkcija yra apibrėžta viduje stdlib.h antraštės failą. Jis turi du parametrus, ne. blokų ir kiekvieno bloko dydžio. Kai dinaminė atmintis paskirstoma naudojant calloc() funkciją, ji grąžina pirmojo bloko bazinį adresą, o kiekvienas blokas inicijuojamas 0. O jei atmintis nesukuriama, grąžinama NULL rodyklė.

Pavyzdžiui, tarkime, kad norime sukurti tris atminties blokus naudodami calloc() funkciją, turime perduoti du parametrus: blokų skaičių (3) ir kiekvieno bloko dydį (int, char, float ir kt.). baitas. Tokiu būdu kompiuterio atmintyje sukuriami trys vienodo dydžio blokai.

Sintaksė

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

Aukščiau pateiktoje sintaksėje funkcija calloc() turi du parametrus. Pirmasis parametras apibrėžia blokų skaičius o antrasis parametras apibrėžia kiekvieno bloko dydis atmintyje. Blokų dydis ir cast_type gali būti int, char, float ir kt.

Grįžti : grąžina pirmojo bloko bazinį adresą ptr kintamajam.

Programa, skirta patikrinti dinaminę atmintį, paskirstoma naudojant calloc() funkciją

Parašykime paprastą programą, kad patikrintume, ar dinaminė atmintis yra skirta C.

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

Išvestis:

 Memory is created successfully 

Programa, skirta parodyti, kaip naudojama calloc() funkcija

Apsvarstykite galimybę sukurti dinaminį atminties paskirstymą naudojant calloc() funkciją ir saugoti duomenis atminties blokuose.

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

Programa, skirta atlaisvinti dinaminį atminties paskirstymą naudojant funkciją free ().

nemokama() funkcija: Funkcija free () naudojama dinaminei atminčiai, kuri yra sukurta, atlaisvinti calloc () arba malloc () funkcija. Šių priskirtų prisiminimų negalima atlaisvinti ir jie išliks iki programos pabaigos. Taigi, mūsų pareiga yra atlaisvinti tą atmintį, kurią galima panaudoti pakartotinai, todėl atminčiai atlaisvinti aiškiai naudojame funkciją free().

Sintaksė

 free (ptr); 

Čia free () yra funkcija, kuri išlaisvina skirtą atmintį naudojant rodyklės ptr kintamąjį.

Apsvarstykite galimybę sukurti dinaminį atminties paskirstymą naudojant funkciją calloc() ir tada atlaisvinti užimtą vietą naudojant C programos funkciją free().

Išleidimas.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></=>