Govt Exams
realloc() attempts to expand memory in place if possible; if not, allocates new block, copies old data, and frees old block.
In queue implementations using linked lists, NULL front pointer indicates queue is empty as there are no nodes to dequeue.
calloc(n, size) allocates n*size bytes and initializes all to 0, while malloc(size) allocates size bytes with garbage values.
A 2D array created with double pointers requires freeing each row first, then the pointer array itself to avoid memory leaks.
malloc allocates exactly sizeof(int) * 10 = 4 * 10 = 40 bytes. sizeof() operator returns size in bytes.
Checking if malloc returns NULL before using the pointer is essential error handling. If allocation fails, the program should handle it gracefully rather than proceeding with NULL.
Doubling capacity and using realloc() provides amortized O(1) insertion with manageable memory overhead, which is the standard approach for dynamic data structures.
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.