Malloc in C
This section will discuss the allocation of the Dynamic memory using the malloc in the C programming language. The malloc is a predefined library function that stands for memory allocation. A malloc is used to allocate a specified size of memory block at the run time of a program. It means it creates a dynamic memory allocation at the run time when the user/programmer does not know the amount of memory space is needed in the program.
Therefore, it inputs the memory size (in bytes) at the run time to reserve a contiguous memory block that returns a pointer of type void, which is cast into a pointer of any form. The dynamic memory is created using the malloc does not initialize the memory at execution time, and hence the memory block contains some default garbage value. The malloc function is defined inside the stdlib.h header file. So, we need to use <stdlib.h> header file while using the malloc function in our program.
In the above syntax, the byte_size is an argument that specifies the size of the memory block (in byte), which is passed into the malloc function to reserve the contiguous memory. A malloc() function returns a type void pointer that can be cast into pointers of any defined form.
Program to check memory is created using the malloc function
Let's consider an example to check memory is created using the malloc function in the C programming language.
Memory is created using the malloc() function
In the above program, we create an integer type dynamic memory using the malloc() function that returns an integer pointer to point to the base address. And if the statement checks whether the ptr is equal to NULL pointer and if the statement is true, it means the memory is not created. Else the memory is successfully created through the malloc() function.
Program to create a dynamic memory using the malloc() function
Let's consider an example of taking the size as an input from the user and then entering data at the run time using the malloc () function in the C programming language.
Enter the allocated size of memory 10 Enter numbers from the user: 25 40 20 13 56 78 67 24 10 7 Numbers are stores in contiguous memory: The number is: 25 The number is: 40 The number is: 20 The number is: 13 The number is: 56 The number is: 78 The number is: 67 The number is: 24 The number is: 10 The number is: 7 Memory is created using the malloc() function
Release memory space using the free() function
A free() function releases the dynamic memory allocation created through the malloc() function. The dynamic memory could not free the occupied memory itself, and the existing space remains even program end. Therefore, we need to free the reserved memory so that other programs can reuse it.
In the above syntax, we pass ptr inside the free() function that acts as the reference or base address of the dynamic memory to points to the memory block.
The number of elements to be entered: 10 Memory is created using the malloc() function Enter the elements in allocated space: 45 12 67 89 34 56 25 25 67 34 Elements are: 45 12 67 89 34 56 25 25 67 34 The addition of stored elements is: 454