logo

Dinamični niz v C

Dinamični nizi so močna podatkovna struktura v programiranju, ki omogoča ustvarjanje in manipuliranje nizi različnih velikosti med izvajanjem. V C so dinamična polja implementirana z uporabo kazalcev in funkcij za dodeljevanje pomnilnika, zaradi česar so dragoceno orodje za optimizacijo uporabe pomnilnika in ustvarjanje učinkovitih programov. V tem članku bomo raziskali koncept dinamičnih nizov v C, njihove prednosti in slabosti ter kako jih ustvariti in manipulirati z njimi.

Razumevanje dinamičnih nizov

A dinamični niz je niz, katerega velikost je mogoče spreminjati med čas izvajanja . Za razliko od statični nizi , ki imajo fiksno velikost, ki je določena v času prevajanja, je mogoče dinamičnim nizom po potrebi spremeniti velikost. Omogoča večjo prilagodljivost in boljše upravljanje pomnilnika, saj je velikost matrike mogoče prilagoditi količini shranjenih podatkov.

Dinamična polja so implementirana z uporabo kazalcev in funkcij za dodeljevanje pomnilnika. V C so najpogosteje uporabljene funkcije za dodeljevanje pomnilnika malloc() , calloc() , in realloc() . Te funkcije omogočajo dodeljevanje in sprostitev pomnilnika med izvajanjem, kar je potrebno za ustvarjanje in upravljanje dinamičnih nizov.

Prednosti dinamičnih nizov

Uporaba dinamičnih nizov v C ima več prednosti. Nekatere glavne prednosti so naslednje:

  1. Ena glavnih prednosti je, da omogočajo boljše upravljanje pomnilnika. Pri statičnih nizih je velikost niza fiksno , kar pomeni, da je pomnilnik dodeljen za celotno polje hkrati. Če matrika ni v celoti izkoriščena, lahko pride do izgubljenega pomnilnika.
  2. Pri dinamičnih nizih se pomnilnik dodeli samo po potrebi, kar lahko privede do učinkovitejše uporabe pomnilnika.
  3. Dinamični nizi omogočajo tudi večjo prilagodljivost.
  4. Lahko je omejujoč, zlasti če se mora velikost matrike spremeniti med izvajanjem.
  5. Dinamična polja omogočajo prilagajanje velikosti polja po potrebi, kar lahko naredi programe bolj vsestranske in prilagodljive.

Slabosti dinamičnih nizov

Medtem ko imajo dinamični nizi številne prednosti, imajo tudi nekaj slabosti. Nekatere glavne pomanjkljivosti so naslednje:

Igralka Rakul Preet Singh
  1. Ena od glavnih pomanjkljivosti je, da so lahko bolj zapleteni za izvedbo kot statična polja.
  2. Dinamični nizi zahtevajo uporabo kazalci in funkcije dodeljevanja pomnilnika , ki je lahko težje razumeti in uporabljati kot preprosto matrično sintakso statičnih matrik.
  3. Dinamični nizi so lahko tudi počasnejši od statičnih nizov. Ker gre za dodeljevanje in sprostitev pomnilnika, so z uporabo dinamičnih nizov povezani režijski stroški. Zaradi teh režijskih stroškov so lahko v nekaterih primerih dinamična polja počasnejša od statičnih.

Ustvarjanje dinamičnih nizov v C

Če želite ustvariti dinamično polje v C, moramo uporabiti funkcije dodeljevanja pomnilnika za dodelitev pomnilnika za polje. Najpogosteje uporabljene funkcije za dodeljevanje pomnilnika v C so malloc(), calloc() , in realloc() . Tukaj je primer, kako ustvariti dinamično polje z uporabo malloc():

'abc's v številkah'
 int *arr; int size = 10; arr = (int*) malloc(size * sizeof(int)); 

Pojasnilo:

V tem primeru deklariramo kazalec na matriko celih števil, imenovano prir . Razglasimo tudi celoštevilsko spremenljivko, imenovano velikost , ki predstavlja velikost matrike, ki jo želimo ustvariti. Po tem uporabimo malloc() funkcijo za dodelitev pomnilnika za polje. The malloc() funkcija prevzame velikost matrike (in bajtov ) kot njegov argument, tako da velikost matrike pomnožimo z velikostjo celega števila (ki je 4 bajti v večini sistemov), da dobite skupno velikost v bajtih.

Manipulacija dinamičnih nizov v C

Ko ustvarimo dinamično polje v jeziku C, lahko z njim manipuliramo tako kot z vsakim drugim poljem. Do posameznih elementov matrike lahko dostopamo s sintakso matrike:

 arr[0] = 5; 

V tem primeru smo prvi element matrike nastavili na 5 .

Lahko tudi uporabimo zanke za ponavljanje po matriki:

 for (int i = 0; i<size; i++) { arr[i]="i" * 2; } < pre> <p>In this example, we use a <strong> <em>for loop</em> </strong> to set each element of the array to twice its index.</p> <p>To resize a dynamic array in C, we can use the <strong> <em>realloc()</em> </strong> function. The <strong> <em>realloc()</em> </strong> function takes two arguments: a <strong> <em>pointer</em> </strong> to the original memory block and the <strong> <em>new size</em> </strong> of the memory block. Here is an example of how to resize a dynamic array using realloc():</p> <pre> int new_size = 20; arr = (int*) realloc(arr, new_size * sizeof(int)); </pre> <p>In this example, we declare a new integer variable called <strong> <em>new_size</em> </strong> , which represents the new size of the array. After that, we use the <strong> <em>realloc() function</em> </strong> to resize the array. The <strong> <em>realloc() function</em> </strong> takes the pointer to the original memory block (in this case, <strong> <em>arr</em> </strong> ) and the <strong> <em>new size</em> </strong> of the memory block (in <strong> <em>bytes</em> </strong> ). We multiply the <strong> <em>new size</em> </strong> of the array by the <strong> <em>size</em> </strong> of an <strong> <em>integer</em> </strong> to get the total size in bytes.</p> <p>It is important to note that when we resize a dynamic array using <strong> <em>realloc()</em> </strong> , any existing data in the array will be preserved. If the new size of the array is larger than the original size, the new elements will be uninitialized.</p> <p>To free the memory used by a dynamic array in C, we can use the <strong> <em>free()</em> </strong> function. The <strong> <em>free()</em> </strong> function takes a pointer to the memory block that was allocated using <strong> <em>malloc()</em> </strong> , <strong> <em>calloc()</em> </strong> , or <strong> <em>realloc()</em> </strong> . Here is an example of how to free the memory used by a dynamic array:</p> <pre> free(arr); </pre> <p>In this example, we use the <strong> <em>free() function</em> </strong> to free the memory used by the dynamic array <strong> <em>arr</em> </strong> . It is important to note that once we have freed the memory used by a dynamic array, we should not attempt to access the elements of the array.</p> <h3>Some more examples of using dynamic arrays in C:</h3> <p> <strong>Adding Elements to a Dynamic Array:</strong> </p> <p>One of the main benefits of using a dynamic array is the ability to add elements to the array as needed. Here is an example of how to add an element to a dynamic array:</p> <pre> #include #include int main() { int size = 5; int *arr = (int*) malloc(size * sizeof(int)); int i; for(i = 0; i<size; i++) { arr[i]="i;" } add a new element to the array size++; arr="(int*)" realloc(arr, size * sizeof(int)); arr[size-1]="i;" for(i="0;" i< size; printf('%d ', arr[i]); free(arr); return 0; < pre> <p> <strong>Output:</strong> </p> <pre> 0 1 2 3 4 5 </pre> <p> <strong>Explanation:</strong> </p> <p>In this example, we first create a dynamic array <strong> <em>arr</em> </strong> of size <strong> <em>5</em> </strong> using the <strong> <em>malloc()</em> </strong> function. After that, we set each element of the array to its index using a <strong> <em>for loop</em> </strong> . To add a new element to the array, we increment the size of the array by one and use the <strong> <em>realloc() function</em> </strong> to resize the array. We set the value of the last element in the array to the current value of <strong> <em>i</em> </strong> . Finally, we print the contents of the array and free the memory used by the array.</p> <h3>Resizing a Dynamic Array</h3> <p>Another advantage of using a dynamic array is the ability to resize the array as needed. Here is an example of how to resize a dynamic array:</p> <pre> #include #include int main() { int size = 5; int *arr = (int*) malloc(size * sizeof(int)); int i; for(i = 0; i<size; i++) { arr[i]="i;" } resize the array size="10;" arr="(int*)" realloc(arr, * sizeof(int)); for(i="5;" i< size; printf('%d ', arr[i]); free(arr); return 0; < pre> <p> <strong>Output:</strong> </p> <pre> 0 1 2 3 4 5 6 7 8 9 </pre> <p> <strong>Explanation:</strong> </p> <p>In this example, we first create a dynamic array <strong> <em>arr</em> </strong> of size <strong> <em>5</em> </strong> using the <strong> <em>malloc() function</em> </strong> . After that, we set each element of the array to its index using a <strong> <em>for loop</em> </strong> . To resize the array, we set the value of size to <strong> <em>10</em> </strong> and use the <strong> <em>realloc()</em> </strong> function to resize the array. After that, we set the value of the new elements in the array using another for loop. Finally, we print the contents of the array and free the memory used by the array.</p> <h2>Conclusion</h2> <p> <strong> <em>Dynamic arrays</em> </strong> are a powerful data structure in programming that allow for the creation and manipulation of arrays of varying sizes during runtime. In C, dynamic arrays are implemented using pointers and memory allocation functions, making them a valuable tool for optimizing memory usage and creating efficient programs.</p> <p>While <strong> <em>dynamic arrays</em> </strong> have many advantages, they also have some disadvantages. Dynamic arrays can be more complex to implement than static arrays and can be slower in some cases. However, the flexibility and efficiency of dynamic arrays make them a valuable tool for many programming tasks.</p> <p>To create and manipulate dynamic arrays in C, we must use memory allocation functions to allocate and deallocate memory during runtime. The most commonly used memory allocation functions in C are <strong> <em>malloc()</em> </strong> , <strong> <em>calloc()</em> </strong> , and <strong> <em>realloc()</em> </strong> . It is important to properly manage memory usage when working with dynamic arrays to avoid memory leaks and other memory-related issues.</p> <hr></size;></pre></size;></pre></size;>

V tem primeru deklariramo novo celoštevilsko spremenljivko, imenovano nova_velikost , ki predstavlja novo velikost matrike. Po tem uporabimo funkcijo realloc(). da spremenite velikost polja. The funkcijo realloc(). premakne kazalec na izvirni pomnilniški blok (v tem primeru prir ) in nova velikost pomnilniškega bloka (in bajtov ). Pomnožimo nova velikost matrike s strani velikost od an celo število da dobite skupno velikost v bajtih.

replaceall v nizu java

Pomembno je omeniti, da ko spreminjamo velikost dinamičnega polja z uporabo realloc() , bodo vsi obstoječi podatki v matriki ohranjeni. Če je nova velikost matrike večja od izvirne velikosti, bodo novi elementi neinicializirani.

Za sprostitev pomnilnika, ki ga uporablja dinamično polje v C, lahko uporabimo prost() funkcijo. The prost() funkcija prevzame kazalec na pomnilniški blok, ki je bil dodeljen z uporabo malloc() , calloc() , oz realloc() . Tukaj je primer, kako sprostiti pomnilnik, ki ga uporablja dinamično polje:

 free(arr); 

V tem primeru uporabljamo funkcija free(). da sprostite pomnilnik, ki ga uporablja dinamično polje prir . Pomembno je vedeti, da ko smo sprostili pomnilnik, ki ga uporablja dinamično polje, ne smemo poskušati dostopati do elementov polja.

Še nekaj primerov uporabe dinamičnih nizov v C:

Dodajanje elementov v dinamični niz:

Ena od glavnih prednosti uporabe dinamične matrike je možnost dodajanja elementov v matriko po potrebi. Tukaj je primer, kako dodate element v dinamično polje:

 #include #include int main() { int size = 5; int *arr = (int*) malloc(size * sizeof(int)); int i; for(i = 0; i<size; i++) { arr[i]="i;" } add a new element to the array size++; arr="(int*)" realloc(arr, size * sizeof(int)); arr[size-1]="i;" for(i="0;" i< size; printf(\'%d \', arr[i]); free(arr); return 0; < pre> <p> <strong>Output:</strong> </p> <pre> 0 1 2 3 4 5 </pre> <p> <strong>Explanation:</strong> </p> <p>In this example, we first create a dynamic array <strong> <em>arr</em> </strong> of size <strong> <em>5</em> </strong> using the <strong> <em>malloc()</em> </strong> function. After that, we set each element of the array to its index using a <strong> <em>for loop</em> </strong> . To add a new element to the array, we increment the size of the array by one and use the <strong> <em>realloc() function</em> </strong> to resize the array. We set the value of the last element in the array to the current value of <strong> <em>i</em> </strong> . Finally, we print the contents of the array and free the memory used by the array.</p> <h3>Resizing a Dynamic Array</h3> <p>Another advantage of using a dynamic array is the ability to resize the array as needed. Here is an example of how to resize a dynamic array:</p> <pre> #include #include int main() { int size = 5; int *arr = (int*) malloc(size * sizeof(int)); int i; for(i = 0; i<size; i++) { arr[i]="i;" } resize the array size="10;" arr="(int*)" realloc(arr, * sizeof(int)); for(i="5;" i< size; printf(\'%d \', arr[i]); free(arr); return 0; < pre> <p> <strong>Output:</strong> </p> <pre> 0 1 2 3 4 5 6 7 8 9 </pre> <p> <strong>Explanation:</strong> </p> <p>In this example, we first create a dynamic array <strong> <em>arr</em> </strong> of size <strong> <em>5</em> </strong> using the <strong> <em>malloc() function</em> </strong> . After that, we set each element of the array to its index using a <strong> <em>for loop</em> </strong> . To resize the array, we set the value of size to <strong> <em>10</em> </strong> and use the <strong> <em>realloc()</em> </strong> function to resize the array. After that, we set the value of the new elements in the array using another for loop. Finally, we print the contents of the array and free the memory used by the array.</p> <h2>Conclusion</h2> <p> <strong> <em>Dynamic arrays</em> </strong> are a powerful data structure in programming that allow for the creation and manipulation of arrays of varying sizes during runtime. In C, dynamic arrays are implemented using pointers and memory allocation functions, making them a valuable tool for optimizing memory usage and creating efficient programs.</p> <p>While <strong> <em>dynamic arrays</em> </strong> have many advantages, they also have some disadvantages. Dynamic arrays can be more complex to implement than static arrays and can be slower in some cases. However, the flexibility and efficiency of dynamic arrays make them a valuable tool for many programming tasks.</p> <p>To create and manipulate dynamic arrays in C, we must use memory allocation functions to allocate and deallocate memory during runtime. The most commonly used memory allocation functions in C are <strong> <em>malloc()</em> </strong> , <strong> <em>calloc()</em> </strong> , and <strong> <em>realloc()</em> </strong> . It is important to properly manage memory usage when working with dynamic arrays to avoid memory leaks and other memory-related issues.</p> <hr></size;></pre></size;>

Pojasnilo:

V tem primeru najprej ustvarimo dinamično polje prir velikosti 5 uporabljati malloc() funkcijo. Nato nastavimo vsak element matrike na njegov indeks z uporabo a za zanko . Da dodamo nov element v matriko, povečamo velikost matrike za eno in uporabimo funkcijo realloc(). da spremenite velikost polja. Vrednost zadnjega elementa v matriki nastavimo na trenutno vrednost jaz . Nazadnje natisnemo vsebino matrike in sprostimo pomnilnik, ki ga uporablja matrika.

kat timpf teža

Spreminjanje velikosti dinamične matrike

Druga prednost uporabe dinamične matrike je možnost spreminjanja velikosti matrike po potrebi. Tukaj je primer, kako spremeniti velikost dinamične matrike:

 #include #include int main() { int size = 5; int *arr = (int*) malloc(size * sizeof(int)); int i; for(i = 0; i<size; i++) { arr[i]="i;" } resize the array size="10;" arr="(int*)" realloc(arr, * sizeof(int)); for(i="5;" i< size; printf(\'%d \', arr[i]); free(arr); return 0; < pre> <p> <strong>Output:</strong> </p> <pre> 0 1 2 3 4 5 6 7 8 9 </pre> <p> <strong>Explanation:</strong> </p> <p>In this example, we first create a dynamic array <strong> <em>arr</em> </strong> of size <strong> <em>5</em> </strong> using the <strong> <em>malloc() function</em> </strong> . After that, we set each element of the array to its index using a <strong> <em>for loop</em> </strong> . To resize the array, we set the value of size to <strong> <em>10</em> </strong> and use the <strong> <em>realloc()</em> </strong> function to resize the array. After that, we set the value of the new elements in the array using another for loop. Finally, we print the contents of the array and free the memory used by the array.</p> <h2>Conclusion</h2> <p> <strong> <em>Dynamic arrays</em> </strong> are a powerful data structure in programming that allow for the creation and manipulation of arrays of varying sizes during runtime. In C, dynamic arrays are implemented using pointers and memory allocation functions, making them a valuable tool for optimizing memory usage and creating efficient programs.</p> <p>While <strong> <em>dynamic arrays</em> </strong> have many advantages, they also have some disadvantages. Dynamic arrays can be more complex to implement than static arrays and can be slower in some cases. However, the flexibility and efficiency of dynamic arrays make them a valuable tool for many programming tasks.</p> <p>To create and manipulate dynamic arrays in C, we must use memory allocation functions to allocate and deallocate memory during runtime. The most commonly used memory allocation functions in C are <strong> <em>malloc()</em> </strong> , <strong> <em>calloc()</em> </strong> , and <strong> <em>realloc()</em> </strong> . It is important to properly manage memory usage when working with dynamic arrays to avoid memory leaks and other memory-related issues.</p> <hr></size;>

Pojasnilo:

V tem primeru najprej ustvarimo dinamično polje prir velikosti 5 uporabljati funkcijo malloc(). . Po tem nastavimo vsak element matrike na njegov indeks z uporabo a za zanko . Če želite spremeniti velikost matrike, nastavimo vrednost velikosti na 10 in uporabite realloc() funkcijo za spreminjanje velikosti polja. Po tem nastavimo vrednost novih elementov v matriki z drugo zanko for. Nazadnje natisnemo vsebino matrike in sprostimo pomnilnik, ki ga uporablja matrika.

java hashmap

Zaključek

Dinamični nizi so močna podatkovna struktura v programiranju, ki omogoča ustvarjanje in manipulacijo nizov različnih velikosti med izvajanjem. V C so dinamična polja implementirana z uporabo kazalcev in funkcij za dodeljevanje pomnilnika, zaradi česar so dragoceno orodje za optimizacijo uporabe pomnilnika in ustvarjanje učinkovitih programov.

Medtem dinamični nizi imajo številne prednosti, imajo pa tudi nekaj slabosti. Dinamična polja so lahko bolj zapletena za implementacijo kot statična polja in so lahko v nekaterih primerih počasnejša. Vendar pa so zaradi prilagodljivosti in učinkovitosti dinamičnih polj dragoceno orodje za številna programska opravila.

Za ustvarjanje in upravljanje dinamičnih nizov v C moramo uporabiti funkcije za dodeljevanje pomnilnika za dodeljevanje in sprostitev pomnilnika med izvajanjem. Najpogosteje uporabljene funkcije za dodeljevanje pomnilnika v C so malloc() , calloc() , in realloc() . Pomembno je, da pri delu z dinamičnimi nizi pravilno upravljate uporabo pomnilnika, da preprečite uhajanje pomnilnika in druge težave, povezane s pomnilnikom.