QP School

Full Version: Dynamic memory allocation in C (malloc, free)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
malloc() is used to allocate memory dynamically, and free() is used to release memory when it's no longer needed.

int *ptr = (int *)malloc(sizeof(int));
if (ptr != NULL) {
    *ptr = 10;
    printf("Value: %d\n", *ptr); // Output: 10
    free(ptr); // Release memory
}