Malloc function in C
When the user or programmer does not know how much memory space is needed in the application, it a needs dynamic memory allocation during runtime.
Dynamic memory allocation helps us to use memory efficiently by enabling us to allocate Memory continuously during runtime rather than in advance. This makes us use the memory space effectively and helps us stay in Memory.
The predefined library function known as Malloc standsfor memory allocation.
When a program runs, Malloc is used to allocate a memory block of a specific size.
The characteristics of the Malloc () function allow us to allocate Memory at some point during runtime. These characteristics are defined inside the header.
The characteristic requires a length in bytes as input and, upon successful memory allocation, provides a void pointer that may be cast to any other kind.
The C malloc() function provides a void type pointer, allowing us to assign dynamic Memory for any data type (primitive or user-defined). A NULL pointer will be returned if the minimum amount of memory space is not available.
Syntax of Malloc () Function in C
ptr = ( cast_ type *) malloc (byte_size);
Return Value of Malloc () Function in C
The C language's Malloc function only allocates one memory block at a time, and the assigned memory address carries the initial garbage cost.
A void pointer corresponding to the Memory allocated by the C malloc function is the return value or type of the Malloc () function in C. The advantage of using a void pointer as the return type is that it allows us to do this for any data type pointer.
Parameters of Malloc() function
The number of bytes of Memory that will be allocated is the sole argument required by the C language's Malloc function. Because the size of each data type might vary depending on the compiler, the sizeof() method is typically used to determine how many bytes are needed.
Free():-
The Memory allotted during runtime does not automatically release itself. Memory allocated at runtime can be manually released using the free() function.
The same header has a description for the free() function. The attribute accepts a pointer and releases the Memory that is pointing to the reference.
Dynamic Memory, often known as heap memory, is the Memory that is allocated to variables when the C programming language's malloc() or calloc() operations are used.
When a programmer defines a variable for a primitive or user-defined data type, static Memory or stack memory is allotted to it.
A program to check if memory is created using the Malloc () function.
#include <stdio.h>
#include <stdlib.h>
int main ()
{
// declare a pointer of type int //
int *pt;
// use the Malloc () function to define the size of the block in bytes
pt = malloc (sizeof(int));
// use if the condition that defines ptr is not equal to null
if (pt != NULL)
{
printf ( " Memory is created using the malloc() function ");
}
else
{
printf ( " memory is not created ");
}
return 0;
}
OUTPUT

Program to create a dynamic memory using the Malloc () function
#include <stdio.h>
#include <stdlib.h>
int main ()
{
// declare the local variables //
int *ptr, size, i;
printf (" Enter the allocated size of memory ");
scanf (" %d", &size);
// use the Malloc () function to define the size of the block in bytes //
ptr = malloc (size * sizeof(int));
// use if the condition that defines ptr is not equal to null //
if (ptr != NULL)
{
// get input from the user and print it //
printf (" Enter numbers from the user: ");
for ( i = 0; i < size; i++)
{
scanf (" %d", ptr + i);
// stores the numbers from base address of memory //
}
printf (" Numbers are stores in contiguous memory: \n ");
for ( i = 0; i < size; i++)
{
printf (" \n The number is: %d", *(ptr + i));
// here *(ptr + i) is same as ptr[i]
}
printf (" \n Memory is created using the malloc() function ");
return 0;
}
else
printf (" memory is not created ");
return 0;
}
OUTPUT

free() function program using Malloc()
#include <stdio.h>
#include <stdlib.h>
int main()
{
int* ptr;
ptr = (int*)malloc(5* sizeof(int));
if (ptr==NULL)
{
printf( "Memory not allocated \n");
}
else
{
printf( "Memory allocated successfully \n ");
for (int i=0; i<5; i++)
{
ptr[i]=i;
}
printf("The elements of the •array are: ");
for (int i=0; i<5; i++)
{
printf("%d \n", ptr[i]);
}
}
free(ptr);
return 0;
}
OUTPUT

free() function program using Calloc()
#include <stdio.h>
#include <stdlib.h>
int main()
{
int* ptr;
int n;
printf(" Enter the elements you want ");
scanf("%d",&n);
ptr = (int*)calloc(n, sizeof(int));
if (ptr==NULL)
{
printf( "Memory not allocated \n");
}
else
{
printf( "Memory allocated successfully \n ");
printf(" Enter the values ");
for (int i=0; i<5; i++)
{
scanf("%d" ,&ptr[i]);
}
printf("The elements of the •array are: ");
for (int i=0; i<5; i++)
{
printf("%d \n", ptr[i]);
}
}
free(ptr);
return 0;
}
OUTPUT
