Govt Exams
<stdlib.h> is the standard header file containing declarations for malloc(), calloc(), realloc(), and free() functions.
malloc() is the standard C library function for dynamic memory allocation. It takes the number of bytes to allocate as an argument and returns a pointer to the allocated memory.
Single allocation with index tracking minimizes malloc/free overhead, suitable for competitive programming time constraints.
"Hello World" is 11 bytes (including null terminator). Writing to 10-byte buffer causes buffer overflow, corrupting adjacent memory.
Large allocations may fail due to heap fragmentation or insufficient contiguous memory. Always check malloc() return for NULL.
Reading input size first, then allocating exact size is optimal for memory efficiency and avoids repeated realloc() overhead.
This shows safe practice: allocation, use, freeing, and immediately setting pointer to NULL to avoid use-after-free.
Adjacency list uses dynamic allocation proportional to V+E, while matrix uses fixed V² space regardless of actual edges.
realloc() returns a new pointer (old location may move). Must use: p = realloc(p, newsize) and check for NULL.
Safe practice includes allocating exact size (input length + 1 for null terminator), checking for NULL, and using bounded string functions.