Malloc In C

Syntax

Example

ptr = ( cast_ type *) malloc (byte_size);

In the provided syntax, the byte_size serves as a parameter defining the memory block's size (in bytes) allocated by the malloc function to secure a continuous memory space. Upon execution, malloc provides a void pointer that is convertible into pointers of various specified types.

Program to check memory is created using the malloc function

Let's explore a scenario where memory allocation is achieved using the malloc function within the C programming language.

Program1.c

Example

#include <stdio.h>
#include <stdlib.h> // use stdlib.h header file to malloc() function
int main ()
{
	int *pt; // declare a pointer of type int
	// use malloc() function to define the size of block in bytes
	pt = malloc (sizeof(int));
	
	// use if 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

Output

Memory is created using the malloc() function

In the program above, we allocate dynamic memory of integer type by utilizing the malloc function, which provides an integer pointer pointing to the base memory address. Subsequently, the code verifies if the ptr is equivalent to the NULL pointer; if this condition evaluates as true, it indicates that the memory allocation was unsuccessful. Conversely, if the condition is false, it signifies that the memory allocation through the malloc function was successful.

Program to create a dynamic memory using the malloc function

Let's explore a scenario where the program prompts the user to input a size value, and then dynamically allocates memory for data input during runtime by utilizing the malloc function in the C programming language.

Program2.c

Example

#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); // get the size of a memory	
	// use malloc() function to define the size of block in bytes
	ptr = malloc (size * sizeof(int));	
	// use if condition that defines ptr is not equal to null
	if (ptr != NULL)
	{
		// get an 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

Output

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 is responsible for releasing the dynamically allocated memory that was created using the malloc function. Dynamic memory allocation does not automatically release the memory it has occupied, so the memory remains allocated even after the program has finished executing. It is essential to free up this allocated memory to make it available for reuse by other programs.

Syntax

Example

free (ptr);

In the provided syntax, we supply ptr as an argument within the free function, which serves as the pointer to the starting address of the dynamically allocated memory block.

Program3.c

Example

#include <stdio.h>
#include <stdlib.h>
void main()
{
	int num, i, sum = 0;
	int *ptr, *p;
	printf (" The number of elements to be entered: \n ");
	scanf (" %d", &num); // define the no. of element to be entered	
	// use malloc() function to define the size of memory block
	ptr = (int *) malloc (num);
	p = ptr; // store the base address of ptr in p	
	// validate the string is full or not
	if (ptr == NULL)
	{
		printf (" Memory is not allocated ");
		exit(0); // exit from program
	}
	else
	{
		printf (" Memory is created using the malloc() function ");
	}
	printf ("\n Enter the elements in allocated space: ");	
	for (i = 1; i <= num; i++)
	{
		scanf (" %d", ptr);
		sum = sum + *ptr;
		ptr++;
	}	
	// print the entered number
	printf (" Elements are: ");
	for (i = 1; i <= num; i++)
	{
		printf (" \n %d", *p);
		p++;
	}	
	printf (" \n The addition of stored elements is: %d ", sum);
	free (ptr); // use free() function to release the occupy space using malloc() function
	getch();	
}

Output

Output

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

Input Required

This code uses input(). Please provide values below: