Explanation
The program demonstrates dynamic memory allocation techniques in C using the values entered by the user. It covers memory allocation using malloc
, calloc
, and realloc
functions.
Header
The code begins with including the standard input-output and standard library header files using #include <stdio.h>
and #include <stdlib.h>
.
Variable Declaration
The main()
function is defined, and several variables are declared:
int d, m, j, o;
: Variables to hold the size values entered by the user for different memory allocation functions.void *e;
: Pointer to hold the allocated memory address.
Program Description
The program prints a description message for the user to understand its functionality.
Malloc Allocation
The program allocates memory using malloc
:
printf("Enter the size of memory to allocate using malloc: ");
: Prompts the user to enter the size of memory to allocate usingmalloc
.scanf("%d", &d);
: Reads the size entered by the user and stores it ind
.e = malloc(d);
: Allocates memory of sized
bytes usingmalloc
.if (e == NULL)
: Checks if memory allocation failed.printf("Error allocating memory\n");
: Prints an error message if memory allocation failed.return 1;
: Exits the program with an error code.
Calloc Allocation
The program allocates memory using calloc
:
printf("Enter the number of elements and size of each element to allocate using calloc: ");
: Prompts the user to enter the number of elements and size of each element to allocate usingcalloc
.scanf("%d %d", &m, &j);
: Reads the values entered by the user and stores them inm
andj
.e = calloc(m, j);
: Allocates memory for an array ofm
elements, each of sizej
bytes, usingcalloc
.if (e == NULL)
: Checks if memory allocation failed.printf("Error allocating memory\n");
: Prints an error message if memory allocation failed.return 1;
: Exits the program with an error code.
Realloc Allocation
The program reallocates memory using realloc
:
printf("Enter the new size to allocate using realloc: ");
: Prompts the user to enter the new size to allocate usingrealloc
.scanf("%d", &o);
: Reads the new size entered by the user and stores it ino
.e = realloc(e, o);
: Reallocates memory to the new sizeo
bytes usingrealloc
.if (e == NULL)
: Checks if memory allocation failed.printf("Error allocating memory\n");
: Prints an error message if memory allocation failed.return 1;
: Exits the program with an error code.
Free Memory
The program frees the allocated memory:
free(e);
: Frees the memory allocated bymalloc
,calloc
, orrealloc
.
Program End
The program ends with return 0;
to indicate successful execution.
Code
/*
* -----------------------------------------------------------
* Logic Building with Computer Programming (CSU1128)
* Instructor: Dr. Pankaj Vaidya | Author: Divya Mohan
*
* This code is a part of the educational initiative by dmj.one
* with aim of empowering and inspiring learners in the field of
* Computer Science and Engineering through the respective courses.
*
* (c) 2022, Divya Mohan for dmj.one. All rights reserved.
* -----------------------------------------------------------
*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("\n\n Program to demonstrate the dynamic memory allocation techniques in C by the values entered by the user. \n\n");
// Declare variables
int d, m, j, o, n;
void *e;
// Get user input for malloc
printf("Enter the size of memory to allocate using malloc: ");
scanf("%d", &d);
// Allocate memory using malloc
e = malloc(d);
if (e == NULL)
{
printf("Error allocating memory\n");
return 1;
}
// Get user input for calloc
printf("Enter the number of elements and size of each element to allocate using calloc: ");
scanf("%d %d", &m, &j);
// Allocate memory using calloc
e = calloc(m, j);
if (e == NULL)
{
printf("Error allocating memory\n");
return 1;
}
// Get user input for realloc
printf("Enter the new size to allocate using realloc: ");
scanf("%d", &o);
// Reallocate memory using realloc
e = realloc(e, o);
if (e == NULL)
{
printf("Error allocating memory\n");
return 1;
}
// Free the allocated memory
free(e);
return 0;
}
Output
Program to demonstrate the dynamic memory allocation techniques in C by the values entered by the user.
Enter the size of memory to allocate using malloc: 5
Enter the number of elements and size of each element to allocate using calloc: 12 43
Enter the new size to allocate using realloc: 23