Govt Exams
int *p = (int*)malloc(5 * sizeof(int));
printf("%d", sizeof(p));
sizeof(p) returns the size of pointer itself (4 bytes on 32-bit, 8 bytes on 64-bit), not the allocated memory.
realloc() shrinks or expands memory. If new size is smaller, it reduces allocation and returns pointer to resized block.
malloc() returns a generic pointer (void*) which can be cast to any data type pointer.
stdlib.h contains malloc(), calloc(), realloc(), and free() functions for dynamic memory management.
struct Node { int data; int next; };
Both A and B allocate memory correctly; B includes explicit typecasting which is optional in C but good practice.
Disciplined memory management with matching alloc/free pairs and proper error handling prevents leaks.
int *p = malloc(INT_MAX);
malloc() returns NULL if allocation fails (insufficient memory); always check the return value.
Fragmentation occurs when memory is allocated and freed irregularly, leaving unused gaps that waste space.
malloc() returns NULL (pointer value 0) on failure; always check before using the pointer.
char *str = malloc(5);
strcpy(str, "Hello World");
Buffer overflow occurs (11 chars into 5 bytes), and memory is never freed, causing a leak.