0
0
Cprogramming~20 mins

Why dynamic memory is needed - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Dynamic Memory Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use dynamic memory allocation in C?

Which of the following best explains why dynamic memory allocation is needed in C programming?

ATo improve the speed of the program by using static memory only.
BTo allocate memory at compile time for fixed-size variables.
CTo avoid using pointers and arrays in the program.
DTo allocate memory during program execution when the size is not known beforehand.
Attempts:
2 left
💡 Hint

Think about when the program needs memory but does not know how much it will need before running.

Predict Output
intermediate
2:00remaining
Output of dynamic memory allocation example

What is the output of the following C code?

C
#include <stdio.h>
#include <stdlib.h>

int main() {
    int *ptr = (int *)malloc(3 * sizeof(int));
    if (ptr == NULL) {
        printf("Memory allocation failed\n");
        return 1;
    }
    ptr[0] = 10;
    ptr[1] = 20;
    ptr[2] = 30;
    printf("%d %d %d\n", ptr[0], ptr[1], ptr[2]);
    free(ptr);
    return 0;
}
A10 20 30
BMemory allocation failed
C0 0 0
DCompilation error
Attempts:
2 left
💡 Hint

Check if memory allocation was successful and values assigned before printing.

Predict Output
advanced
2:00remaining
What happens if malloc fails?

What will be the output of this C program if malloc fails to allocate memory?

C
#include <stdio.h>
#include <stdlib.h>

int main() {
    int *ptr = (int *)malloc((size_t)1000000000000 * sizeof(int));
    if (ptr == NULL) {
        printf("Allocation failed\n");
    } else {
        printf("Allocation succeeded\n");
        free(ptr);
    }
    return 0;
}
AAllocation succeeded
BAllocation failed
CCompilation error due to large size
DProgram crashes without output
Attempts:
2 left
💡 Hint

Consider what happens when malloc cannot find enough memory.

🧠 Conceptual
advanced
2:00remaining
Why is dynamic memory preferred over static for large data?

Why is dynamic memory allocation preferred over static allocation for large or variable-sized data in C?

AStatic memory is faster but limited in size and fixed at compile time.
BDynamic memory is slower and fixed in size at compile time.
CDynamic memory allocation does not require freeing memory after use.
DStatic memory can only store integers, dynamic memory can store any data type.
Attempts:
2 left
💡 Hint

Think about when the program size or data size is not known before running.

Predict Output
expert
2:00remaining
Value of pointer after free() call

What is the value of ptr after calling free(ptr); in this program?

C
#include <stdio.h>
#include <stdlib.h>

int main() {
    int *ptr = malloc(sizeof(int));
    *ptr = 42;
    free(ptr);
    printf("%p\n", (void *)ptr);
    return 0;
}
ANULL
B0
CAddress of the freed memory block (non-NULL pointer)
DCompilation error because ptr is used after free
Attempts:
2 left
💡 Hint

Does free() change the pointer variable itself?