logo

Calloc w C

W tym temacie omówiono sposób tworzenia dynamicznej alokacji pamięci przy użyciu funkcji calloc() w języku programowania C. Zanim przejdziemy przez koncepcje, omówmy dynamiczną alokację pamięci w C. Pamięć dynamiczna to procedura programowania strukturalnego, która pozwala użytkownikom alokować pamięć w czasie wykonywania programu. Korzystając z dynamicznej alokacji pamięci, możemy zwiększyć lub zmniejszyć pamięć podczas wykonywania programu. W ten sposób unika się marnowania pamięci komputera. Alokacja pamięci jest podzielona na dwie części: funkcję malloc() i calloc().

Calloc w C

A funkcja calloc(). to predefiniowana funkcja biblioteczna, która oznacza ciągła alokacja pamięci . Funkcja calloc() służy do tworzenia wielu bloków w czasie wykonywania programu o tym samym rozmiarze w pamięci. Funkcja calloc jest zdefiniowana wewnątrz pliku stdlib.h plik nagłówkowy. Ma dwa parametry, nie. bloków i wielkość każdego bloku. Kiedy pamięć dynamiczna jest alokowana za pomocą funkcji calloc(), zwraca ona adres bazowy pierwszego bloku, a każdy blok jest inicjalizowany wartością 0. A jeśli pamięć nie jest utworzona, zwraca wskaźnik NULL.

ile uncji to 10 mililitrów

Załóżmy na przykład, że chcemy utworzyć trzy bloki pamięci za pomocą funkcji calloc(), musimy przekazać dwa parametry, liczbę bloków (3) i rozmiar każdego bloku (int, char, float itp.) w bajt. W ten sposób tworzy w pamięci komputera trzy bloki o tej samej wielkości.

Składnia

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

W powyższej składni funkcja calloc() ma dwa parametry. Pierwszy parametr definiuje liczba bloków a drugi parametr definiuje wielkość każdego bloku w pamięci. Rozmiar bloków i typ_obsady mogą być wyrażone w int, char, float itp.

znak do int

Powrót : Zwraca adres bazowy pierwszego bloku do zmiennej ptr.

Program sprawdzający pamięć dynamiczną przydzielany jest za pomocą funkcji calloc().

Napiszmy prosty program sprawdzający, czy pamięć dynamiczna jest przydzielona w C.

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

Wyjście:

 Memory is created successfully 

Program demonstrujący użycie funkcji calloc().

Rozważmy utworzenie dynamicznej alokacji pamięci przy użyciu funkcji calloc() i przechowywanie danych w blokach pamięci.

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 zwalniający dynamiczną alokację pamięci przy użyciu funkcji free().

funkcja bezpłatna(): Funkcja free() służy do zwalniania utworzonej pamięci dynamicznej kaloc () Lub malloc () funkcja. Te przydzielone pamięci nie mogą zostać uwolnione i będą istnieć do końca programu. Zatem naszym obowiązkiem jest zwolnienie tej pamięci, którą można ponownie wykorzystać, dlatego jawnie używamy funkcji free() w celu zwolnienia pamięci.

„co” to 10 z 100

Składnia

 free (ptr); 

Tutaj free() jest funkcją zwalniającą przydzieloną pamięć za pomocą zmiennej ptr wskaźnika.

blokuj reklamy na YouTube na Androida

Rozważmy utworzenie dynamicznej alokacji pamięci za pomocą funkcji calloc(), a następnie zwolnienie zajętego miejsca za pomocą funkcji free() w programie C.

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