Home Subjects C Programming Dynamic Memory

C Programming
Dynamic Memory

C language from basics to advanced placement prep

100 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 81–90 of 100
Topics in C Programming
Q.81 Easy Dynamic Memory
Which header file must be included to use malloc(), calloc(), and free() functions?
A #include
B #include
C #include
D #include
Correct Answer:  B. #include
EXPLANATION

All memory allocation and deallocation functions are declared in <stdlib.h>.

Take Test
Q.82 Easy Dynamic Memory
How much memory is allocated by malloc(sizeof(int) * 10) on a 32-bit system where sizeof(int) = 4?
A 10 bytes
B 40 bytes
C 4 bytes
D 44 bytes
Correct Answer:  B. 40 bytes
EXPLANATION

sizeof(int) * 10 = 4 * 10 = 40 bytes on a 32-bit system.

Take Test
Q.83 Hard Dynamic Memory
What does this realloc() call do?
int *p = malloc(5 * sizeof(int));
p = realloc(p, 10 * sizeof(int));
A Allocates new 10-integer block, discards old data
B Expands existing block to 10 integers, preserves old data
C Frees old block and allocates new one
D Returns error
Correct Answer:  B. Expands existing block to 10 integers, preserves old data
EXPLANATION

realloc() resizes the memory block while preserving existing data. If expansion in-place fails, it allocates new block and copies data.

Take Test
Q.84 Hard Dynamic Memory
What is the correct way to allocate memory for 2D dynamic array (3x4)?
A int arr[3][4] = malloc(...);
B int **arr = malloc(3 * sizeof(int*)); for each row: malloc(4 * sizeof(int));
C int *arr = malloc(12 * sizeof(int));
D int arr = malloc(3*4*sizeof(int));
Correct Answer:  B. int **arr = malloc(3 * sizeof(int*)); for each row: malloc(4 * sizeof(int));
EXPLANATION

For true 2D dynamic array, allocate pointer array first, then allocate each row. Option C is 1D linear allocation.

Take Test
Q.85 Hard Dynamic Memory
What is the issue in this code?
void func() {
int *p = malloc(sizeof(int));
*p = 5;
}
int main() {
func();
// p is not accessible here
return 0;
}
A Syntax error
B Memory leak - allocated memory is never freed
C Stack overflow
D No issue
Correct Answer:  B. Memory leak - allocated memory is never freed
EXPLANATION

The pointer p is local to func(). Memory is allocated but never freed, causing a memory leak.

Take Test
Q.86 Hard Dynamic Memory
Which statement about dynamic memory is TRUE?
A Dynamic memory is faster than stack memory
B Dynamic memory persists until explicitly freed
C Dynamic memory is limited by cache size
D Dynamic memory is automatically freed at function end
Correct Answer:  B. Dynamic memory persists until explicitly freed
EXPLANATION

Dynamic memory persists until free() is called, unlike automatic variables which are freed at function end.

Take Test
Q.87 Hard Dynamic Memory
What will happen if malloc() fails and returns NULL but code doesn't check?
A Program automatically allocates from stack
B Dereferencing NULL causes undefined behavior/crash
C Memory is automatically reallocated
D Exception is raised
Correct Answer:  B. Dereferencing NULL causes undefined behavior/crash
EXPLANATION

Dereferencing a NULL pointer causes undefined behavior, typically resulting in segmentation fault or program crash.

Take Test
Q.88 Medium Dynamic Memory
What is the output?
int *p = malloc(sizeof(int) * 3);
p[0] = 1; p[1] = 2; p[2] = 3;
printf("%d", *(p+2));
A 1
B 2
C 3
D Compilation error
Correct Answer:  C. 3
EXPLANATION

*(p+2) is equivalent to p[2], which contains 3. Pointer arithmetic works with dynamic arrays.

Take Test
Q.89 Medium Dynamic Memory
Analyze this code:
char *str = malloc(5);
strcpy(str, "Hello");
How many bytes should malloc allocate for safety?
A 5
B 6
C 4
D 7
Correct Answer:  B. 6
EXPLANATION

String "Hello" has 5 characters plus 1 null terminator (\0), requiring 6 bytes total.

Take Test
Q.90 Medium Dynamic Memory
What is double free error?
A Allocating memory twice
B Calling free() on already freed memory
C Using two pointers to same memory
D Allocating memory twice and freeing once
Correct Answer:  B. Calling free() on already freed memory
EXPLANATION

Double free error occurs when free() is called on the same memory block twice, causing undefined behavior and program crash.

Take Test
IGET
iget AI
Online · Ask anything about exams
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips