0% found this document useful (0 votes)
3 views

Dynamic Memory Program

The document provides examples of dynamic memory allocation in C using malloc(), calloc(), and realloc(). It demonstrates how to allocate memory for an array, check for successful allocation, and print the elements of the array. Additionally, it includes error handling for memory allocation failures.

Uploaded by

shivgovindvns
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Dynamic Memory Program

The document provides examples of dynamic memory allocation in C using malloc(), calloc(), and realloc(). It demonstrates how to allocate memory for an array, check for successful allocation, and print the elements of the array. Additionally, it includes error handling for memory allocation failures.

Uploaded by

shivgovindvns
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Example of malloc() in c printf("The elements of the array are: ");

for (i = 0; i < n; ++i) {

#include <stdio.h> printf("%d, ", ptr[i]);

#include<conio.h> }

#include <stdlib.h> }

void main()

{ getch();

// This pointer will hold the }

// base address of the block created

int* ptr;

int n, i;

// Get the number of elements for the array

printf("Enter number of elements:");

scanf("%d",&n);

printf("Entered number of elements: %d\n", n);

// Dynamically allocate memory using malloc()

ptr = (int*)malloc(n * sizeof(int));

// Check if the memory has been successfully

// allocated by malloc or not

if (ptr == NULL) {

printf("Memory not allocated.\n");

exit(0);

else {

// Memory has been successfully allocated

printf("Memory successfully allocated using


malloc.\n");

// Get the elements of the array

for (i = 0; i < n; ++i) {

ptr[i] = i + 1;

// Print the elements of the array


Example of calloc() in C

// Get the elements of the array

#include <stdio.h> for (i = 0; i < n; ++i) {

#include<conio.h> ptr[i] = i + 1;

#include <stdlib.h> }

void main() // Print the elements of the array

{ printf("The elements of the array are: ");

for (i = 0; i < n; ++i) {

// This pointer will hold the printf("%d, ", ptr[i]);

// base address of the block created }

int* ptr; }

int n, i; getch();

clrscr(); }

// Get the number of elements for the array

n = 5;

printf("Enter number of elements: %d\n", n);

// Dynamically allocate memory using calloc()

ptr = (int*)calloc(n, sizeof(int));

// Check if the memory has been successfully

// allocated by calloc or not

if (ptr == NULL) {

printf("Memory not allocated.\n");

exit(0);

else {

// Memory has been successfully allocated

printf("Memory successfully allocated using


calloc.\n");
Example of realloc() in C printf("The elements of the array are: ");

for (i = 0; i < n; ++i) {

#include <stdio.h> printf("%d, ", ptr[i]);

#include<conio.h> }

#include <stdlib.h> // Get the new size for the array

void main() n = 10;

{ printf("\n\nEnter the new size of the array: %d\n",


n);
// This pointer will hold the
// Dynamically re-allocate memory using realloc()
// base address of the block created
ptr = (int*)realloc(ptr, n * sizeof(int));
int* ptr;
if (ptr == NULL) {
int n, i;
printf("Reallocation Failed\n");
clrscr();
exit(0);
// Get the number of elements for the array
}
n = 5;
// Memory has been successfully allocated
printf("Enter number of elements: %d\n", n);
printf("Memory successfully re-allocated using
// Dynamically allocate memory using calloc()
realloc.\n");
ptr = (int*)calloc(n, sizeof(int));
// Get the new elements of the array
// Check if the memory has been successfully
for (i = 5; i < n; ++i) {
// allocated by malloc or not
ptr[i] = i + 1;
if (ptr == NULL) {
}
printf("Memory not allocated.\n");
// Print the elements of the array
exit(0);
printf("The elements of the array are: ");
}
for (i = 0; i < n; ++i) {
else {
printf("%d, ", ptr[i]);
// Memory has been successfully allocated
}
printf("Memory successfully allocated using
free(ptr);
calloc.\n");
}
// Get the elements of the array
getch();
for (i = 0; i < n; ++i) {
}
ptr[i] = i + 1;

// Print the elements of the array

You might also like