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 51–60 of 100
Topics in C Programming
Q.51 Medium Dynamic Memory
What is the correct way to allocate a dynamic array of struct for 'n' elements?
struct Node { int data; char name[20]; };
A struct Node *arr = malloc(n);
B struct Node *arr = malloc(n * sizeof(struct Node));
C struct Node arr = malloc(n);
D struct Node *arr = calloc(n, 1);
Correct Answer:  B. struct Node *arr = malloc(n * sizeof(struct Node));
EXPLANATION

Must allocate n * sizeof(struct Node) bytes to hold all n structures.

Take Test
Q.52 Medium Dynamic Memory
In competitive exams, why is dynamic allocation preferred over static arrays for unknown input sizes?
A It's faster than static arrays
B Allows allocation only as much memory as needed, preventing stack overflow
C Static arrays don't work in C
D It's automatically freed by compiler
Correct Answer:  B. Allows allocation only as much memory as needed, preventing stack overflow
EXPLANATION

Dynamic allocation allocates memory on heap based on runtime input, preventing fixed-size limitations and stack overflow.

Take Test
Q.53 Hard Dynamic Memory
Consider this code:
int *p = malloc(sizeof(int));
int *q = p;
free(p);
q[0] = 5; // What is the result?
A Valid assignment
B Compiler error
C Use-after-free: undefined behavior
D Memory automatically reallocated
Correct Answer:  C. Use-after-free: undefined behavior
EXPLANATION

Both p and q point to freed memory. Writing to q after free(p) is undefined behavior.

Take Test
Q.54 Medium Dynamic Memory
If realloc() cannot expand memory at the current location, what does it do?
A Fails and returns NULL
B Allocates new memory, copies old data, frees old memory, returns new pointer
C Extends memory anyway
D Keeps old pointer valid
Correct Answer:  B. Allocates new memory, copies old data, frees old memory, returns new pointer
EXPLANATION

realloc() finds new space if needed, copies data, deallocates old block, and returns new pointer.

Take Test
Q.55 Medium Dynamic Memory
What is the output of this code?
int *p = (int*)calloc(3, sizeof(int));
printf("%d %d %d", p[0], p[1], p[2]);
A Garbage values
B 0 0 0
C 3 bytes each
D Compiler error
Correct Answer:  B. 0 0 0
EXPLANATION

calloc() initializes all allocated memory to zero, so all elements will be 0.

Take Test
Q.56 Medium Dynamic Memory
Which scenario best demonstrates a memory leak?
A char *s = malloc(10); free(s);
B char *s = malloc(10); s = malloc(20);
C char *s = malloc(10); free(s); free(s);
D char *s = NULL; free(s);
Correct Answer:  B. char *s = malloc(10); s = malloc(20);
EXPLANATION

Original malloc(10) pointer is lost before freeing. The 10 bytes are now inaccessible - a memory leak.

Take Test
Q.57 Easy Dynamic Memory
How much memory in bytes does calloc(10, 4) allocate?
A 10 bytes
B 4 bytes
C 40 bytes
D 10.4 bytes
Correct Answer:  C. 40 bytes
EXPLANATION

calloc(num, size) allocates num * size = 10 * 4 = 40 bytes total.

Take Test
Q.58 Medium Dynamic Memory
What happens if you use a pointer after calling free() on it (without reallocating)?
A Program runs normally
B Pointer becomes NULL
C Use-after-free: undefined behavior, possible crash or data corruption
D Compiler automatically prevents it
Correct Answer:  C. Use-after-free: undefined behavior, possible crash or data corruption
EXPLANATION

After free(), accessing the pointer causes undefined behavior as memory is returned to the heap and may be reused.

Take Test
Q.59 Medium Dynamic Memory
In dynamic 2D array creation:
int **arr = (int**)malloc(m * sizeof(int*));
what does the first malloc() allocate?
A m rows of integers
B An array of m pointers to int
C m*n integers
D Memory for entire 2D array at once
Correct Answer:  B. An array of m pointers to int
EXPLANATION

First malloc allocates an array of m pointers. Each pointer must be individually allocated in a loop for actual row data.

Take Test
Q.60 Medium Dynamic Memory
Which statement about free() is correct?
A free() must always be called exactly once per malloc()
B free() sets the pointer to NULL automatically
C Calling free() twice causes undefined behavior
D free() returns 1 on success, 0 on failure
Correct Answer:  C. Calling free() twice causes undefined behavior
EXPLANATION

Double-free causes undefined behavior (segmentation fault). free() doesn't set pointer to NULL; programmer must do it explicitly.

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