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.42Medium
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.43Medium
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.44Medium
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.45Medium
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.
Advertisement
Q.46Medium
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.47Medium
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.48Medium
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.49Medium
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.50Medium
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.51Medium
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.
Q.52Medium
In competitive programming for graph problems, if adjacency list uses dynamic arrays, what is the time complexity of adding an edge?
Answer: A
Using dynamic arrays (vectors in C++) with doubling strategy provides O(1) amortized time for appending edges.
Q.53Medium
What is the correct way to store variable-length strings dynamically?
Answer: B
Proper way requires dynamic allocation with malloc, adding 1 for null terminator, and using fgets for safe input.