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.422Medium
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.423Medium
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.424Medium
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.425Medium
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.
Advertisement
Q.426Medium
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.427Medium
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.428Medium
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.429Medium
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.
Q.430Medium
Which scenario represents a use-after-free error?
Answer: A
In option A, after free(p), the memory is deallocated. Accessing it with p[0] = 5 is a use-after-free error, leading to undefined behavior.
Q.431Medium
For storing variable-length strings dynamically in a competitive program, what's the safest approach?
Answer: A
Strings in C are null-terminated, so allocating strlen(s)+1 bytes ensures space for the null terminator. This is the standard safe approach.
Q.432Medium
What potential issue occurs with this code in a large-scale system?
while(1) { int *arr = malloc(1000000); }
Answer: A
This creates an infinite loop that allocates 1MB each iteration without freeing, rapidly exhausting available memory. This is a classic memory leak scenario.
Q.433Medium
In graph algorithms with dynamic adjacency lists, what's the memory requirement for a sparse graph with V vertices and E edges?
Answer: A
Using dynamic adjacency lists, you allocate V list pointers and E edge nodes total, giving O(V + E) space complexity, which is optimal for sparse graphs.
Q.434Medium
What is the advantage of dynamic allocation over static allocation in competitive programming?
Answer: A
Dynamic allocation allows using memory proportional to actual input size, preventing TLE from stack overflow with large static arrays and enabling solutions for variable-sized problems.
Q.435Medium
Which function should be used to allocate memory for an array of 50 structures of size 'sizeof(struct Node)' and initialize to zero?
Answer: A
calloc(50, sizeof(struct Node)) allocates memory for 50 elements of struct Node size and initializes all bytes to zero, which is ideal when you need initialized memory.
Q.436Medium
What happens when you try to free a pointer that was not allocated by malloc/calloc/realloc?
Answer: A
Calling free() on a pointer that wasn't returned by malloc/calloc/realloc causes undefined behavior, potentially corrupting the heap and crashing the program.
Q.437Medium
Which of the following correctly deallocates a 2D dynamically allocated array created as int **arr?
Answer: B
A 2D array created with double pointers requires freeing each row first, then the pointer array itself to avoid memory leaks.
Q.438Medium
What happens when realloc() is called on a pointer allocated by malloc() with a larger size?
Answer: C
realloc() attempts to expand memory in place if possible; if not, allocates new block, copies old data, and frees old block.
Q.439Medium
For implementing a dynamic circular buffer of size n, how many pointers are minimally required?
Answer: B
A circular buffer requires head and tail pointers minimum to track current positions in the circular structure.
Q.440Medium
Which scenario is most likely to cause a segmentation fault in dynamic memory?
Answer: B
Buffer overflow by accessing beyond allocated boundaries causes segmentation fault as it accesses invalid memory pages.