Memory Management

 Memory Management


Memory management is crucial for efficient resource utilization. C provides manual memory management through functions like malloc, calloc, realloc, and free. Understanding memory management allows you to allocate and deallocate memory dynamically.



#include <stdio.h>

#include <stdlib.h>


int main() {

    int* numbers = (int*)malloc(5 * sizeof(int));


    if (numbers == NULL) {

        printf("Memory allocation failed.\n");

        return 1;

    }


    for (int i = 0; i < 5; i++) {

        numbers[i] = i + 1;

    }


    for (int i = 0; i < 5; i++) {

        printf("%d ", numbers[i]);

    }


    free(numbers);


    return 0;

}

No comments:

Post a Comment

The Importance of Cybersecurity in the Digital Age

 The Importance of Cybersecurity in the Digital Age Introduction: In today's digital age, where technology is deeply intertwined with ev...