Memory management
suggest changeThe standard library declares its dynamic-memory functions in <stdlib.h>:
void *malloc(size_t size);allocates uninitialized storage.void *calloc(size_t count, size_t size);allocates storage and initializes every bit to zero.void *realloc(void *ptr, size_t new_size);resizes an allocation.void free(void *ptr);releases an allocation.void *aligned_alloc(size_t alignment, size_t size);requests a stricter alignment and is available since C11.
Check allocation results before dereferencing them. Guard multiplication used to calculate an allocation size against SIZE_MAX overflow, or use calloc, which must detect overflow in count * size.
aligned_alloc requires a valid alignment supported by the implementation. C11 as originally published required size to be an integral multiple of alignment; a later defect correction removed that restriction, but rounding up remains useful when targeting older implementations. Storage returned by aligned_alloc can be released with free.
alloca is not part of ISO C. It allocates implementation-defined stack storage that is released only when the calling function returns, cannot report failure portably, and can exhaust limited stack space. Prefer standard allocation functions or an automatic array with a known, bounded size.