Dynamic memory allocation (malloc, calloc, realloc, free)

Dynamic memory allocation (malloc, calloc, realloc, free)

Dynamic memory allocation in C programming allows the program to allocate new memory, reallocate existing memory, and free memory space allocated by the malloc function when memory is requested at runtime. This makes it more flexible and easy to manage different types of program variable data and to efficiently manage storage memory. Some of the popular functions used for dynamic memory allocation in C programs are malloc() function, calloc() function, realloc() function, and free() function.

Dynamic memory allocation (malloc, calloc, realloc, free)

Examples of dynamic memory allocation in C programs.

Using malloc (memory allocation) in C.

The malloc() function allocates a new block size of memory of the defined size in new space bytes in the C program.

syntax –

void* malloc(size_t size);

size – is the number of bytes to allocate the new space block in the existing C program.

Here memory allocation returns void* pointer or this function returns NULL value if memory allocation fails.

Example of malloc() in C.

#include <stdio.h>

#include <stdlib.h>

int main() {

int *ptr = (int *)malloc(7 * sizeof(int)); // it allocates memory with array of 7 integer

if (ptr == NULL) {

printf(“\n Memory allocation process is unsucessfull!”);

return 1;

}

for (int p = 0; p < 7; p++) {

ptr[p] = p * 5; // assign the value to p

printf(“%d “, ptr[p]); // print the p array variable value

}

printf(“\n”);

free(ptr); // free() function free the allocated memory by maclloc function

return 0;

}

calloc (contiguous allocation) in C.

The calloc function is used to allocate continuous memory for array elements. Using the calloc function, all bytes of the array are initialized to zero. Remember that calloc function allocates memory to zero.

Syntax of calloc() in c.

void* calloc(size_t value, size_t size);

value – Number of elements with calloc.

size – the Size of each element in bytes.

Here calloc returns a void* pointer to the allocated memory, or returns a NULL value if allocation fails.

Example of calloc() in C.

#include <stdio.h>

#include <stdlib.h>

int main() {

int *ptr = (int *)calloc(7, sizeof(int)); // it Allocates memory for 7 integer array start with 0

if (ptr == NULL) {

printf(“\n Memory allocation unsucessfull!”);

return 1;

}

for (int p = 0; p < 7; p++) {

printf(“%d “, ptr[p]); // it read and print the array values

printf(“\n”);

free(ptr); // free() function Free the allocated memory in array

return 0;

}

realloc (reallocate) in C.

The realloc() function in C program replaces the size of the memory block already allocated in the program with the new size. The realloc function manages the content to the minimum of the old and new sizes.

realloc (reallocate) in C.

void* realloc(void* ptr, size_t size);

ptr – Here ptr is the pointer value of the previously allocated memory block.

size – Here size is the new size in bytes of previously allocated memory.

Realloc() returns a void* pointer to the newly allocated memory, or NULL value if reallocating fails.

Example of realloc() in C.

#include <stdio.h>

#include <stdlib.h>

int main() {

int *ptr = (int *)malloc(7 * sizeof(int)); // it Initialize memory allocation with 7 integers

if (ptr == NULL) {

printf(“\n the Memory allocation is unsuccessfull!”);

return 1;

}

for (int p = 0; p < 7; p++) {

ptr[p] = p * 5; // Assign value to p variable

}

ptr = (int *)realloc(ptr, 12 * sizeof(int)); // it Resize to hold 12 new integers value with realloc function

if (ptr == NULL) {

printf(“\n the Memory reallocation unsuccessfull!”);

return 1;

}

for (int p = 7; p < 12; p++) {

ptr[p] = p * 12; // it Assign new values ​​to array element

}

for (int p = 0; p < 12; p++) {

printf(“%d “, ptr[p]); // it print the all array element values

}

printf(“\n”);

free(ptr); // the free () used to Free the allocated memory in array

return 0;

}

free (deallocation) function in C language.

The free function deallocates the previously allocated memory block from the storage location in the C program. Here the simple use of free() is to free the allocated memory bytes.

Syntax of free() in C.

void free(void* ptr);

ptr – Here ptr variable is a pointer to the memory block to be freed.

There is no return value here.

Example of free() in C.

#include <stdlib.h>

void testFunction() {

int *ptr = (int *)malloc(50 * sizeof(int)); // it allocates 50 byte memory

if (ptr != NULL) {

// use the exisitng memory

free(ptr); // now the free() used to free the memory when we need to free them

ptr = NULL; // assign pointer to null

}

}