logo

Calloc in C

In diesem Thema wird erläutert, wie Sie mithilfe der Funktion calloc() in der Programmiersprache C eine dynamische Speicherzuweisung erstellen. Bevor wir die Konzepte durchgehen, besprechen wir die dynamische Speicherzuweisung in C. Dynamischer Speicher ist ein Strukturprogrammierungsverfahren, das es Benutzern ermöglicht, den Speicher zur Laufzeit eines Programms zuzuweisen. Mithilfe der dynamischen Speicherzuweisung können wir den Speicher während der Ausführung eines Programms vergrößern oder verkleinern. Auf diese Weise wird die Verschwendung von Computerspeicher vermieden. Eine Speicherzuweisung ist in zwei Teile unterteilt: die Funktionen malloc() und calloc().

Calloc in C

A calloc()-Funktion ist eine vordefinierte Bibliotheksfunktion, die für steht zusammenhängende Speicherzuordnung . Eine calloc()-Funktion wird verwendet, um zur Laufzeit eines Programms mehrere Blöcke mit derselben Größe im Speicher zu erstellen. Eine Calloc-Funktion ist innerhalb definiert stdlib.h Header-Datei. Es hat zwei Parameter, nein. Anzahl der Blöcke und die Größe jedes Blocks. Wenn der dynamische Speicher mithilfe der Funktion calloc() zugewiesen wird, wird die Basisadresse des ersten Blocks zurückgegeben und jeder Block wird mit 0 initialisiert. Und wenn kein Speicher erstellt wird, wird ein NULL-Zeiger zurückgegeben.

Angenommen, wir möchten mit der Funktion calloc() drei Speicherblöcke erstellen, müssen wir zwei Parameter, eine Anzahl von Blöcken (3) und die Größe jedes Blocks (int, char, float usw.) übergeben das Byte. Auf diese Weise werden im Computerspeicher drei Blöcke mit gleicher Größe erstellt.

Primärschlüssel, zusammengesetzter Schlüssel

Syntax

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

In der obigen Syntax hat die Funktion calloc() zwei Parameter. Der erste Parameter definiert die Anzahl der Blöcke und der zweite Parameter definiert die Größe jedes Blocks in Erinnerung. Die Größe der Blöcke und cast_type kann in int, char, float usw. angegeben werden.

Zurückkehren : Es gibt die Basisadresse des ersten Blocks an die ptr-Variable zurück.

Programm zur Überprüfung des dynamischen Speichers wird mithilfe der Funktion calloc() zugewiesen

Schreiben wir ein einfaches Programm, um zu überprüfen, ob der dynamische Speicher in C zugewiesen ist.

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

Ausgabe:

Zeichenfolge alle Java ersetzen
 Memory is created successfully 

Programm zur Demonstration der Verwendung der Funktion calloc()

Betrachten wir die Erstellung einer dynamischen Speicherzuweisung mithilfe der Funktion calloc() und das Speichern von Daten in den Speicherblöcken.

Programm2.c

Java mit Swing
 #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></=>

Programm zum Freigeben der dynamischen Speicherzuweisung mithilfe der Funktion free()

free()-Funktion: Eine free()-Funktion wird verwendet, um den erzeugten dynamischen Speicher freizugeben calloc () oder malloc ()-Funktion. Diese zugewiesenen Speicher können nicht für sich freigegeben werden und bleiben bis zum Ende des Programms bestehen. Es liegt also in unserer Verantwortung, den Speicher freizugeben, der wiederverwendet werden kann, und daher verwenden wir explizit die Funktion free(), um den Speicher freizugeben.

Syntax

 free (ptr); 

Hier ist free() eine Funktion, die den zugewiesenen Speicher mithilfe der Zeigervariablen ptr freigibt.

Betrachten wir die Erstellung einer dynamischen Speicherzuweisung mithilfe der Funktion calloc() und die anschließende Freigabe belegten Speicherplatzes mithilfe der Funktion free() im C-Programm.

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