Double-free causes undefined behavior (segmentation fault). free() doesn't set pointer to NULL; programmer must do it explicitly.
Q.22Medium
In dynamic 2D array creation:
int arr = (int)malloc(m * sizeof(int*));
what does the first malloc() allocate?
Answer: B
First malloc allocates an array of m pointers. Each pointer must be individually allocated in a loop for actual row data.
Q.23Medium
What happens if you use a pointer after calling free() on it (without reallocating)?
Answer: C
After free(), accessing the pointer causes undefined behavior as memory is returned to the heap and may be reused.
Q.24Medium
Which scenario best demonstrates a memory leak?
Answer: B
Original malloc(10) pointer is lost before freeing. The 10 bytes are now inaccessible - a memory leak.
Q.25Medium
What is the output of this code?
int *p = (int*)calloc(3, sizeof(int));
printf("%d %d %d", p[0], p[1], p[2]);
Answer: B
calloc() initializes all allocated memory to zero, so all elements will be 0.
Advertisement
Q.26Medium
If realloc() cannot expand memory at the current location, what does it do?
Answer: B
realloc() finds new space if needed, copies data, deallocates old block, and returns new pointer.
Q.27Medium
In competitive exams, why is dynamic allocation preferred over static arrays for unknown input sizes?
Answer: B
Dynamic allocation allocates memory on heap based on runtime input, preventing fixed-size limitations and stack overflow.
Q.28Medium
What is the correct way to allocate a dynamic array of struct for 'n' elements?
struct Node { int data; char name[20]; };
Answer: B
Must allocate n * sizeof(struct Node) bytes to hold all n structures.
Q.29Medium
A competitive programmer allocates a 2D array dynamically. What is the correct approach?
Answer: A
2D arrays are allocated as array of pointers, where each row pointer points to a dynamically allocated array.
Q.30Medium
Consider a program that repeatedly allocates memory in a loop but never frees it. What problem occurs?
Answer: B
Allocating without freeing causes memory leak, where allocated memory is not returned to the system, eventually exhausting heap.
Q.31Medium
What happens if you call free() twice on the same pointer?
Answer: B
Double free is undefined behavior and can cause program crash or corruption. After first free(), the pointer should not be used.
Q.32Medium
In a competitive programming scenario, you need to store strings dynamically. What is safe practice?
Answer: B
Safe practice includes allocating exact size (input length + 1 for null terminator), checking for NULL, and using bounded string functions.
Q.33Medium
A program uses realloc() to expand an array from 100 to 200 elements. The old address is p. After realloc():?
Answer: C
realloc() returns a new pointer (old location may move). Must use: p = realloc(p, newsize) and check for NULL.
Q.34Medium
For a graph with V vertices and E edges stored dynamically, what is typical space complexity?
Answer: B
Adjacency list uses dynamic allocation proportional to V+E, while matrix uses fixed V² space regardless of actual edges.
Q.35Medium
What is the critical issue with this code? int *p = (int*)malloc(sizeof(int)); free(p); p = NULL;
Answer: C
This shows safe practice: allocation, use, freeing, and immediately setting pointer to NULL to avoid use-after-free.
Q.36Medium
In 2024-2025 competitive exams, for storing variable number of test cases (up to 10^5), which approach is optimal?
Answer: B
Reading input size first, then allocating exact size is optimal for memory efficiency and avoids repeated realloc() overhead.
Q.37Medium
In a competitive programming problem, you need to allocate a 2D array of size 5x5 dynamically. Which approach is most efficient?
Answer: A
Allocating a single contiguous block is more cache-efficient and requires only one malloc() call, making it better for competitive programming where memory access speed matters.
Q.38Medium
What is the critical issue with this code snippet?
int *ptr = (int*)malloc(10);
ptr[11] = 5;
Answer: A
malloc(10) allocates 10 bytes, but accessing ptr[11] goes beyond the allocated boundary, causing a buffer overflow which leads to undefined behavior and potential segmentation fault.
Q.39Medium
Which of the following is a memory leak?
Answer: A
In option A, the first malloc(100) block becomes inaccessible after p is reassigned, but it's not freed, causing a memory leak. The original 100 bytes are lost.
Q.40Medium
In the 2024-2025 competitive exam pattern, a dynamic array needs to grow from 100 to 200 elements. Using realloc(), what should be the concern?
Answer: A
If realloc() needs to relocate the memory block to a different address, all old pointers to the array become invalid. Only the returned pointer should be used.