CTSD-2 Unit - 2
CTSD-2 Unit - 2
return 0;
}
calloc()
“calloc” or “contiguous allocation” method in C is
used to dynamically allocate the specified number of
blocks of memory of the specified type.
it is very much similar to malloc() but has two different
points and these are:
It initializes each block with a default value ‘0’.
It has two parameters or arguments as compare to
malloc().
calloc()
Syntax of calloc() in C
ptr = (cast-type*)calloc(n, element-size);
here, n is the no. of elements and element-size is the size of
each element.
For Example:
ptr = (float*) calloc(25, sizeof(float));
This statement allocates contiguous space in memory for 25
elements each with the size of the float.
If space is insufficient, allocation fails and returns a NULL
pointer.
calloc()
calloc()
// Check if the memory has been
Example of calloc() in C successfully
#include <stdio.h> // allocated by calloc or not
#include <stdlib.h> if (ptr == NULL) {
int main() printf("Memory not
{ allocated.\n");
int* ptr; exit(0);
int n, i; }
// Get the number of elements for the else {
array
n = 5; // Memory has been
printf("Enter number of elements: %d\n", successfully allocated
n); printf("Memory successfully
// Dynamically allocate memory using allocated using calloc.\n");
calloc()
ptr = (int*)calloc(n, sizeof(int));
calloc()
// Get the elements of the array
for (i = 0; i < n; ++i) { Enter number of elements: 5
ptr[i] = 5*i + 8; Memory successfully allocated
} using calloc.
The elements of the array are:
// Print the elements of the array 8,13,18,23,28
printf("The elements of the array are: ");
for (i = 0; i < n; ++i) {
printf("%d, ", ptr[i]);
}
}
return 0;
}
realloc()
“realloc” or “re-allocation” method in C is used to
dynamically change the memory allocation of a previously
allocated memory.
In other words, if the memory previously allocated with the
help of malloc or calloc is insufficient, realloc can be used to
dynamically re-allocate memory.
re-allocation of memory maintains the already present
value and new blocks will be initialized with the default
garbage value.
realloc()
Syntax of realloc() in C
ptr = realloc(ptr, newSize);
where ptr is reallocated with new size 'newSize'.
realloc()
#include <stdio.h> // Check if the memory has been
#include <stdlib.h> successfully
int main() // allocated by malloc or not
{ if (ptr == NULL) {
int* ptr; printf("Memory not
int n, i; allocated.\n");
// Get the number of elements for exit(0);
the array }
printf("Enter number of elements: else {
%d\n", n); // Memory has been successfully
n = 5; allocated
// Dynamically allocate memory using printf("Memory successfully
calloc() allocated using calloc.\n");
ptr = (int*)calloc(n, sizeof(int));
realloc()
// Get the elements of the array // Dynamically re-allocate memory
for (i = 0; i < n; ++i) { using realloc()
ptr[i] = i + 1; ptr = (int*)realloc(ptr, n *
} sizeof(int));
// Print the elements of the array
printf("The elements of the array if (ptr == NULL) {
are: "); printf("Reallocation Failed\n");
for (i = 0; i < n; ++i) { exit(0);
printf("%d, ", ptr[i]); }
} / Memory has been successfully
allocated
// Get the new size for the array printf("Memory successfully re-
printf("\n\nEnter the new size of the allocated using realloc.\n");
array: %d\n", n);
n = 10;
realloc()
// Get the new elements of the array Enter number of elements: 5
for (i = 5; i < n; ++i) { Memory successfully allocated using
ptr[i] = i + 1; calloc.
} The elements of the array are: 1, 2, 3, 4, 5,
// Print the elements of the array Enter the new size of the array: 10
printf("The elements of the array are: ");Memory successfully re-allocated using
for (i = 0; i < n; ++i) { realloc.
printf("%d, ", ptr[i]); The elements of the array are: 1, 2, 3, 4, 5,
} 6, 7, 8, 9, 10,
free(ptr);
}
return 0;
}
free()
“free” method in C is used to dynamically de-
allocate the memory.
The memory allocated using functions malloc() and
calloc() is not de-allocated on their own.
Hence the free() method is used, whenever the
dynamic memory allocation takes place.
It helps to reduce wastage of memory by freeing it.
free()
Syntax of free() in C
free(ptr);
free()
#include <stdio.h> // Dynamically allocate memory using calloc()
#include <stdlib.h> ptr1 = (int*)calloc(n, sizeof(int));
int main()
// Check if the memory has been successfully
{
// allocated by malloc or not
int *ptr, *ptr1;
if (ptr == NULL || ptr1 == NULL) {
int n, i; printf("Memory not allocated.\n");
// Get the number of elements for the exit(0);
array }
n = 5; else {
printf("Entered number of elements:
%d\n", n); // Memory has been successfully allocated
// Dynamically allocate memory using printf("Memory successfully allocated
using malloc.\n");
malloc()
ptr = (int*)malloc(n * sizeof(int));
free()
// Free the memory Enter number of elements: 5
free(ptr); Memory successfully allocated using malloc.
printf("Malloc Memory successfully Malloc Memory successfully freed.
freed.\n");
Memory successfully allocated using calloc.
// Memory has been successfully
Calloc Memory successfully freed.
allocated
printf("\nMemory successfully
allocated using calloc.\n");
// Free the memory
free(ptr1);
printf("Calloc Memory successfully
freed.\n");
}
return 0;
}
Application of C
Operating systems: C is widely used for developing operating
systems such as Unix, Linux, and Windows.
Embedded systems: C is a popular language for developing
embedded systems such as microcontrollers, microprocessors, and
other electronic devices.
System software: C is used for developing system software such as
device drivers, compilers, and assemblers.
Networking: C is widely used for developing networking applications
such as web servers, network protocols, and network drivers.
Database systems: C is used for developing database systems such as
Oracle, MySQL, and PostgreSQL.
Application of C
Gaming: C is often used for developing computer games due to its
ability to handle low-level hardware interactions.
Artificial Intelligence: C is used for developing artificial intelligence
and machine learning applications such as neural networks and
deep learning algorithms.
Scientific applications: C is used for developing scientific
applications such as simulation software and numerical analysis
tools.
Financial applications: C is used for developing financial
applications such as stock market analysis and trading systems.
www.paruluniversity.ac.in