Ker je C strukturiran jezik, ima nekaj fiksnih pravil za programiranje. Eden od njih vključuje spreminjanje velikosti matrike. Niz je zbirka elementov, shranjenih na sosednjih pomnilniških lokacijah.
Kot lahko vidite, je dolžina (velikost) zgornje matrike 9. Kaj pa, če obstaja zahteva za spremembo te dolžine (velikosti)? na primer
- Če pride do situacije, ko je treba v to matriko vnesti le 5 elementov. V tem primeru preostali 4 indeksi samo zapravljajo pomnilnik v tej matriki. Zato je treba zmanjšati dolžino (velikost) niza z 9 na 5.
- Vzemite drugo situacijo. V tem je niz 9 elementov z vsemi 9 indeksi zapolnjenih. Toda v to matriko je treba vnesti še 3 elemente. V tem primeru so potrebni še 3 indeksi. Torej je treba dolžino (velikost) niza spremeniti z 9 na 12.
Ta postopek se imenuje Dinamično dodeljevanje pomnilnika v C .
Zato je C Dinamično dodeljevanje pomnilnika lahko definiramo kot postopek, pri katerem se velikost podatkovne strukture (kot je Array) spreminja med izvajanjem.
C nudi nekaj funkcij za doseganje teh nalog. Obstajajo 4 knjižnične funkcije, ki jih ponuja C, definirane pod datoteko glave za lažjo dinamično dodelitev pomnilnika pri programiranju C. To so:
- malloc()
- calloc()
- prost()
- realloc()
Oglejmo si vsakega od njih podrobneje.
Metoda C malloc().
The malloc oz dodeljevanje pomnilnika metoda v C se uporablja za dinamično dodelitev enega velikega bloka pomnilnika z določeno velikostjo. Vrne kazalec tipa void, ki ga je mogoče pretvoriti v kazalec katere koli oblike. V času izvajanja ne inicializira pomnilnika, tako da je na začetku inicializiral vsak blok s privzeto vrednostjo smeti.
Sintaksa malloc() v C
ptr = (cast-type*) malloc(byte-size) For Example:>
ptr = (int*) malloc(100 * sizeof(int));
Ker je velikost int 4 bajte, bo ta stavek dodelil 400 bajtov pomnilnika. In kazalec ptr vsebuje naslov prvega bajta v dodeljenem pomnilniku.
oblikovalski vzorci v Javi
Če prostora ni dovolj, dodelitev ne uspe in vrne kazalec NULL.
Primer malloc() v C
C
#include> #include> int> main()> {> > // This pointer will hold the> > // base address of the block created> > int> * ptr;> > int> n, i;> > // Get the number of elements for the array> > printf> (> 'Enter number of elements:'> );> > scanf> (> '%d'> ,&n);> > printf> (> 'Entered number of elements: %d
'> , n);> > // Dynamically allocate memory using malloc()> > ptr = (> int> *)> malloc> (n *> sizeof> (> int> ));> > // Check if the memory has been successfully> > // allocated by malloc or not> > if> (ptr == NULL) {> > printf> (> 'Memory not allocated.
'> );> > exit> (0);> > }> > else> {> > // Memory has been successfully allocated> > printf> (> 'Memory successfully allocated using malloc.
'> );> > // Get the elements of the array> > for> (i = 0; i ptr[i] = i + 1; } // Print the elements of the array printf('The elements of the array are: '); for (i = 0; i printf('%d, ', ptr[i]); } } return 0; }> |
>
>Izhod
Enter number of elements: 5 Memory successfully allocated using malloc. The elements of the array are: 1, 2, 3, 4, 5,>
C calloc() metoda
- calloc oz sosednja dodelitev Metoda v C se uporablja za dinamično dodeljevanje določenega števila blokov pomnilnika podane vrste. je zelo podoben malloc(), vendar ima dve različni točki in to sta:
- Vsak blok inicializira s privzeto vrednostjo '0'.
- V primerjavi z malloc() ima dva parametra ali argumenta.
Sintaksa calloc() v C
ptr = (cast-type*)calloc(n, element-size); here, n is the no. of elements and element-size is the size of each element.>
Na primer:
ptr = (float*) calloc(25, sizeof(float));
Ta stavek dodeli sosednji prostor v pomnilniku za 25 elementov z velikostjo lebdečega elementa.
Če prostora ni dovolj, dodelitev ne uspe in vrne kazalec NULL.
Primer calloc() v C
C
armstrong številka
#include> #include> int> main()> {> > // This pointer will hold the> > // base address of the block created> > int> * ptr;> > int> n, i;> > // Get the number of elements for the array> > n = 5;> > printf> (> 'Enter number of elements: %d
'> , n);> > // Dynamically allocate memory using calloc()> > ptr = (> int> *)> calloc> (n,> sizeof> (> int> ));> > // Check if the memory has been successfully> > // allocated by calloc or not> > if> (ptr == NULL) {> > printf> (> 'Memory not allocated.
'> );> > exit> (0);> > }> > else> {> > // Memory has been successfully allocated> > printf> (> 'Memory successfully allocated using calloc.
'> );> > // Get the elements of the array> > for> (i = 0; i ptr[i] = i + 1; } // Print the elements of the array printf('The elements of the array are: '); for (i = 0; i printf('%d, ', ptr[i]); } } return 0; }> |
>
>Izhod
Enter number of elements: 5 Memory successfully allocated using calloc. The elements of the array are: 1, 2, 3, 4, 5,>
Metoda C free().
prost metoda v C se uporablja za dinamično razporediti spomin. Pomnilnik, dodeljen s funkcijama malloc() in calloc(), se sam po sebi ne razveljavi. Zato se metoda free() uporablja vedno, ko pride do dinamične dodelitve pomnilnika. Pomaga zmanjšati izgubo pomnilnika tako, da ga sprosti.
Sintaksa free() v C
free(ptr);>
Primer free() v C
C
#include> #include> int> main()> {> > // This pointer will hold the> > // base address of the block created> > int> *ptr, *ptr1;> > int> n, i;> > // Get the number of elements for the array> > n = 5;> > printf> (> 'Enter number of elements: %d
'> , n);> > // Dynamically allocate memory using malloc()> > ptr = (> int> *)> malloc> (n *> sizeof> (> int> ));> > // Dynamically allocate memory using calloc()> > ptr1 = (> int> *)> calloc> (n,> sizeof> (> int> ));> > // Check if the memory has been successfully> > // allocated by malloc or not> > if> (ptr == NULL || ptr1 == NULL) {> > printf> (> 'Memory not allocated.
'> );> > exit> (0);> > }> > else> {> > // Memory has been successfully allocated> > printf> (> 'Memory successfully allocated using malloc.
'> );> > // Free the memory> > free> (ptr);> > printf> (> 'Malloc Memory successfully freed.
'> );> > // Memory has been successfully allocated> > printf> (> '
Memory successfully allocated using calloc.
'> );> > // Free the memory> > free> (ptr1);> > printf> (> 'Calloc Memory successfully freed.
'> );> > }> > return> 0;> }> |
>
>Izhod
Enter number of elements: 5 Memory successfully allocated using malloc. Malloc Memory successfully freed. Memory successfully allocated using calloc. Calloc Memory successfully freed.>
C realloc() metoda
realloc oz prerazporeditev metoda v C se uporablja za dinamično spreminjanje dodelitve pomnilnika predhodno dodeljenega pomnilnika. Z drugimi besedami, če je pomnilnik, predhodno dodeljen s pomočjo malloc ali calloc, nezadosten, se lahko realloc uporabi za dinamično ponovno dodeli pomnilnik . ponovna dodelitev pomnilnika ohrani že obstoječo vrednost, novi bloki pa bodo inicializirani s privzeto vrednostjo smeti.
Sintaksa realloc() v C
ptr = realloc(ptr, newSize); where ptr is reallocated with new size 'newSize'.>
Če prostora ni dovolj, dodelitev ne uspe in vrne kazalec NULL.
Primer realloc() v C
C
#include> #include> int> main()> {> > // This pointer will hold the> > // base address of the block created> > int> * ptr;> > int> n, i;> > // Get the number of elements for the array> > n = 5;> > printf> (> 'Enter number of elements: %d
'> , n);> > // Dynamically allocate memory using calloc()> > ptr = (> int> *)> calloc> (n,> sizeof> (> int> ));> > // Check if the memory has been successfully> > // allocated by malloc or not> > if> (ptr == NULL) {> > printf> (> 'Memory not allocated.
'> );> > exit> (0);> > }> > else> {> > // Memory has been successfully allocated> > printf> (> 'Memory successfully allocated using calloc.
'> );> > // Get the elements of the array> > for> (i = 0; i ptr[i] = i + 1; } // Print the elements of the array printf('The elements of the array are: '); for (i = 0; i printf('%d, ', ptr[i]); } // Get the new size for the array n = 10; printf('
Enter the new size of the array: %d
', n); // Dynamically re-allocate memory using realloc() ptr = (int*)realloc(ptr, n * sizeof(int)); // Memory has been successfully allocated printf('Memory successfully re-allocated using realloc.
'); // Get the new elements of the array for (i = 5; i ptr[i] = i + 1; } // Print the elements of the array printf('The elements of the array are: '); for (i = 0; i printf('%d, ', ptr[i]); } free(ptr); } return 0; }> |
>
starost kylie jenner
>Izhod
Enter number of elements: 5 Memory successfully allocated using calloc. The elements of the array are: 1, 2, 3, 4, 5, Enter the new size of the array: 10 Memory successfully re-allocated using realloc. The elements of the array are: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,>
Še en primer metode realloc() je:
C
#include> #include> int> main()> {> > int> index = 0, i = 0, n,> > *marks;> // this marks pointer hold the base address> > // of the block created> > int> ans;> > marks = (> int> *)> malloc> (> sizeof> (> > int> ));> // dynamically allocate memory using malloc> > // check if the memory is successfully allocated by> > // malloc or not?> > if> (marks == NULL) {> > printf> (> 'memory cannot be allocated'> );> > }> > else> {> > // memory has successfully allocated> > printf> (> 'Memory has been successfully allocated by '> > 'using malloc
'> );> > printf> (> '
marks = %pc
'> ,> > marks);> // print the base or beginning> > // address of allocated memory> > do> {> > printf> (> '
Enter Marks
'> );> > scanf> (> '%d'> , &marks[index]);> // Get the marks> > printf> (> 'would you like to add more(1/0): '> );> > scanf> (> '%d'> , &ans);> > if> (ans == 1) {> > index++;> > marks = (> int> *)> realloc> (> > marks,> > (index + 1)> > *> sizeof> (> > int> ));> // Dynamically reallocate> > // memory by using realloc> > // check if the memory is successfully> > // allocated by realloc or not?> > if> (marks == NULL) {> > printf> (> 'memory cannot be allocated'> );> > }> > else> {> > printf> (> 'Memory has been successfully '> > 'reallocated using realloc:
'> );> > printf> (> > '
base address of marks are:%pc'> ,> > marks);> ////print the base or> > ///beginning address of> > ///allocated memory> > }> > }> > }> while> (ans == 1);> > // print the marks of the students> > for> (i = 0; i <= index; i++) {> > printf> (> 'marks of students %d are: %d
'> , i,> > marks[i]);> > }> > free> (marks);> > }> > return> 0;> }> |
>
>
Izhod: