Govt. Exams
Entrance Exams
Proper way requires dynamic allocation with malloc, adding 1 for null terminator, and using fgets for safe input.
Using dynamic arrays (vectors in C++) with doubling strategy provides O(1) amortized time for appending edges.
Buffer overflow by accessing beyond allocated boundaries causes segmentation fault as it accesses invalid memory pages.
A circular buffer requires head and tail pointers minimum to track current positions in the circular structure.
realloc() attempts to expand memory in place if possible; if not, allocates new block, copies old data, and frees old block.
A 2D array created with double pointers requires freeing each row first, then the pointer array itself to avoid memory leaks.
Calling free() on a pointer that wasn't returned by malloc/calloc/realloc causes undefined behavior, potentially corrupting the heap and crashing the program.
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.
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.
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.