C Program to Find Largest Number Using Dynamic Memory Allocation

Updated on September 30, 2024
Find Largest Number Using Dynamic Memory Allocation header image

Introduction

In C programming, dynamic memory allocation allows us to allocate memory at runtime using pointers. This is particularly useful in scenarios where the memory needed is not known at compile time. Using dynamic memory allocation through functions like malloc() and free(), you can manage arrays and other data structures efficiently.

In this article, you will learn how to create a C program to find the largest number in an array. This program will utilize dynamic memory allocation to handle the array, emphasizing how to work with pointers and memory management effectively.

Handling Dynamic Memory in C

Allocate Memory for the Array

  1. Include the necessary headers and declare the main function.

  2. Use malloc() to allocate memory for the array based on the user's input for its size.

  3. Check if the memory allocation was successful.

    c
    #include <stdio.h>
    #include <stdlib.h>
    
    int main() {
        int n, *ptr, i, max;
    
        printf("Enter the number of elements: ");
        scanf("%d", &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);
        }
    }
    

    In this code, memory for n integers is dynamically allocated. If malloc() fails to allocate memory, it returns NULL, and the program exits with an error message.

Input Array Elements

  1. Use a loop to take n inputs from the user to fill the array.

  2. Validate each input to ensure proper memory allocation and data integrity.

    c
    printf("Enter the elements: ");
    for (i = 0; i < n; ++i) {
        scanf("%d", ptr + i);
    }
    

    This loop stores the input values directly in the memory locations allocated for the array, using the pointer ptr.

Find the Largest Number

  1. Initialize the maximum number with the first element in the array.

  2. Iterate through the array to find the largest number.

    c
    max = *ptr;
    for (i = 1; i < n; i++) {
        if (*(ptr + i) > max) {
            max = *(ptr + i);
        }
    }
    printf("The largest element is %d\n", max);
    

    This code compares each element in the array with the current max value, updating max whenever a larger element is found.

Free the Allocated Memory

  1. After finding the largest number, free the dynamically allocated memory.

  2. Add the appropriate cleanup code to prevent memory leaks.

    c
    free(ptr);
    return 0;
    

    Releasing the memory back to the system is crucial to avoid memory leaks, especially in larger or long-running programs.

Conclusion

The C program demonstrated here effectively finds the largest number in an array while illustrating the use of dynamic memory allocation with malloc() and free(). Managing memory manually is a powerful feature of C, allowing precise control over how memory is used in your programs. Practice these concepts to deepen your understanding and ensure efficient memory usage in more complex applications. Always remember to handle memory with care to avoid leaks and ensure the stability of your programs.