logo

Calloc valodā C

Šajā tēmā tiks apspriests, kā izveidot dinamisku atmiņas piešķiršanu, izmantojot funkciju calloc() programmēšanas valodā C. Pirms jēdzieniem apskatīsim dinamiskās atmiņas piešķiršanu programmā C. Dinamiskā atmiņa ir struktūras programmēšanas procedūra, kas ļauj lietotājiem piešķirt atmiņu programmas izpildes laikā. Izmantojot dinamisko atmiņas piešķiršanu, mēs varam palielināt vai samazināt atmiņu programmas izpildes laikā. Tādā veidā tas ļauj izvairīties no datora atmiņas izšķērdēšanas. Atmiņas piešķiršana ir sadalīta divās daļās: funkcija malloc () un calloc ().

Calloc valodā C

A calloc() funkcija ir iepriekš definēta bibliotēkas funkcija, kas apzīmē blakus esošās atmiņas piešķiršana . Funkciju calloc () izmanto, lai izveidotu vairākus blokus tādas programmas izpildes laikā, kuras atmiņā ir vienāds izmērs. Calloc funkcija ir definēta iekšpusē stdlib.h galvenes fails. Tam ir divi parametri, nē. bloku lielums un katra bloka izmērs. Kad dinamiskā atmiņa tiek piešķirta, izmantojot funkciju calloc(), tā atgriež pirmā bloka bāzes adresi, un katrs bloks tiek inicializēts ar 0. Un, ja atmiņa netiek izveidota, tā atgriež NULL rādītāju.

Piemēram, pieņemsim, ka mēs vēlamies izveidot trīs atmiņas blokus, izmantojot funkciju calloc(), mums ir jānodod divi parametri, bloku skaits (3) un katra bloka lielums (int, char, float utt.). baits. Tādā veidā tas datora atmiņā izveido trīs blokus, kuru izmērs ir vienāds.

Sintakse

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

Iepriekš minētajā sintaksē funkcijai calloc () ir divi parametri. Pirmais parametrs nosaka bloku skaits un otrais parametrs nosaka katra bloka izmērs atmiņā. Bloku lielums un cast_type var būt int, char, float utt.

Atgriezties : Tas atgriež pirmā bloka bāzes adresi ptr mainīgajam.

Programma dinamiskās atmiņas pārbaudei tiek piešķirta, izmantojot funkciju calloc().

Uzrakstīsim vienkāršu programmu, lai pārbaudītu, vai dinamiskā atmiņa ir piešķirta C.

programma.c

veiktspējas pārbaude
 #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; } 

Izvade:

 Memory is created successfully 

Programma, kas demonstrē calloc() funkcijas izmantošanu

Apsvērsim iespēju izveidot dinamisku atmiņas piešķiršanu, izmantojot funkciju calloc() un saglabāt datus atmiņas blokos.

nobīdes augstums

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 dinamiskās atmiņas piešķiršanas atbrīvošanai, izmantojot funkciju free().

bezmaksas() funkcija: Funkcija free () tiek izmantota, lai atbrīvotu izveidoto dinamisko atmiņu calloc () vai malloc () funkcija. Šīs piešķirtās atmiņas nevar tikt atbrīvotas, un tās pastāvēs līdz programmas beigām. Tātad, mūsu pienākums ir atbrīvot šo atmiņu, ko var izmantot atkārtoti, un tāpēc mēs skaidri izmantojam funkciju free(), lai atbrīvotu atmiņu.

Sintakse

 free (ptr); 

Šeit free () ir funkcija, kas atbrīvo piešķirto atmiņu, izmantojot rādītāja ptr mainīgo.

Apsvērsim iespēju izveidot dinamisku atmiņas piešķiršanu, izmantojot funkciju calloc () un pēc tam atbrīvot aizņemto vietu, izmantojot C programmas funkciju free ().

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