A function returns a pointer it got from malloc(). Is the memory still valid in the caller?
Heap memory is not tied to any function. Returning it is fine (unlike returning a local array) – and the caller becomes responsible for freeing it.
Memory Allocation · Exercise 1 of 4
Twelve multiple-choice questions on malloc, calloc, realloc and free, stack vs heap, memmove, alignment and C23. Pick an option to check your answer instantly.
A function returns a pointer it got from malloc(). Is the memory still valid in the caller?
Heap memory is not tied to any function. Returning it is fine (unlike returning a local array) – and the caller becomes responsible for freeing it.
What does aligned_alloc(64, 256) return?
The first argument is the alignment, the second the size (which should be a multiple of the alignment). It is used for SIMD data and cache-line alignment, and the block is released with ordinary free().
Timed online tests with instant scores and explanations.