---
title: Find Largest Number Using Dynamic Memory Allocation
url: https://docs.vultr.com/clang/examples/find-largest-number-using-dynamic-memory-allocation
publish_date: 2024-09-27T18:23:48.925573Z
last_updated: 2024-09-27T18:23:48.925573Z
header_image: https://assets.vultrdevrel.com/docs-main-doc-assets-1/collection-items/574/4f2104ab-d200-4f92-9afb-ceafdc68e0f7.jpg
---

> Index of every Vultr Docs page in markdown: https://docs.vultr.com/llms.txt

# C Program to Find Largest Number Using Dynamic Memory Allocation

## 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.
1. Use `malloc()` to allocate memory for the array based on the user's input for its size.
1. 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.
1. 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.
1. 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.
1. 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.