C Program to Allocate Memory Dynamically Using malloc()

Introduction

Dynamic memory allocation in C allows you to allocate memory at runtime using functions like malloc(). This is useful when you need to allocate memory whose size isn’t known until the program is running. The malloc() function allocates a specified number of bytes and returns a pointer to the first byte of the allocated memory. If the allocation fails, it returns NULL.

Example:

  • Input: Number of integers to store
  • Output: Memory allocated and integers stored in the allocated memory

Problem Statement

Create a C program that:

  • Prompts the user to enter the number of integers they want to store.
  • Allocates memory dynamically using malloc() for the entered number of integers.
  • Stores and displays the integers.

Solution Steps

  1. Include the Standard Libraries: Use #include <stdio.h> for standard input-output functions and #include <stdlib.h> for dynamic memory allocation functions.
  2. Prompt the User for the Number of Integers: Ask the user how many integers they want to store.
  3. Allocate Memory Dynamically Using malloc(): Use malloc() to allocate memory for the number of integers specified by the user.
  4. Check if Memory Allocation was Successful: Ensure the pointer returned by malloc() is not NULL.
  5. Store and Display the Integers: Use a loop to input and display the integers stored in the allocated memory.
  6. Free the Allocated Memory: Use free() to deallocate the memory once it is no longer needed.

C Program to Allocate Memory Dynamically Using malloc()

#include <stdio.h> #include <stdlib.h> int main() { int *ptr; int n, i; // Step 2: Prompt the user for the number of integers printf("Enter the number of integers: "); scanf("%d", &n); // Step 3: Allocate memory dynamically using malloc() ptr = (int*)malloc(n * sizeof(int)); // Step 4: Check if memory allocation was successful if (ptr == NULL) { printf("Memory allocation failed.\n"); return 1; // Exit the program if memory allocation fails } // Step 5: Store and display the integers printf("Enter %d integers:\n", n); for (i = 0; i < n; i++) { scanf("%d", &ptr[i]); } printf("The integers you entered are:\n"); for (i = 0; i < n; i++) { printf("%d ", ptr[i]); } printf("\n"); // Step 6: Free the allocated memory free(ptr); return 0; // Return 0 to indicate successful execution } 

Explanation

Step 2: Prompt the User for the Number of Integers

  • The program asks the user to input the number of integers they want to store, which is then stored in the variable n.

Step 3: Allocate Memory Dynamically Using malloc()

  • The malloc() function is used to allocate memory for n integers. The size of memory allocated is n * sizeof(int), where sizeof(int) is the size of an integer in bytes.
  • The pointer ptr is used to point to the allocated memory.

Step 4: Check if Memory Allocation was Successful

  • The program checks if ptr is NULL. If it is, it means the memory allocation failed, and the program displays an error message and exits.

Step 5: Store and Display the Integers

  • A for loop is used to input the integers from the user and store them in the dynamically allocated memory.
  • Another for loop is used to display the integers stored in the allocated memory.

Step 6: Free the Allocated Memory

  • The free() function is called to deallocate the memory allocated by malloc(). This is important to prevent memory leaks.

Return 0

  • The return 0; statement indicates that the program executed successfully.

Output Example

Example Output:

Enter the number of integers: 5 Enter 5 integers: 1 2 3 4 5 The integers you entered are: 1 2 3 4 5 

Conclusion

This C program demonstrates how to allocate memory dynamically using malloc(). It covers basic concepts such as dynamic memory allocation, pointer manipulation, and memory deallocation, making it a useful example for beginners learning C programming.

Leave a Comment

Scroll to Top