malloc(3)
C library function
Reserves a block of memory on the heap and returns its address, or NULL if the allocation fails. The contents are uninitialized; the block is yours until free.
bytes);
Allocates memory16 bytes on the heap, contents undefined
Returns0x4052a0the start of the blockNULLfailure
Other common calls
- malloc(n * sizeof(int));
- room for
nintegers - malloc(strlen(s) + 1);
- enough to copy
s, with its\0
#include <stdlib.h>
void *malloc(size_t size);Description
malloc asks the system for size contiguous bytes on the heap and returns the address of the first one. The block lives until you hand it back with free(3), whichever function created it: that is what sets the heap apart from the stack, where a variable disappears at the end of its block. The C23 standard (ISO/IEC 9899, 2024 edition) says it plainly: the representation of the block is indeterminate. In other words, the bytes you get contain anything, often leftovers from an earlier allocation. If you need memory set to zero, use calloc(3).
The return type is void *, which converts to any object pointer type without an explicit cast in C. Write int *t = malloc(n * sizeof(int));, with no cast.
Return value
The address of the block on success, NULL on failure. Always test this return value before you dereference the pointer: writing through a NULL pointer triggers a segmentation fault. Failure happens when memory runs out, but also, on Linux, long after malloc returned an address: the kernel overcommits memory by default, so a huge block can look allocated and then get the process killed at the moment you actually write to it.
malloc(0) is an edge case: the standard allows either NULL or a unique pointer that you can pass to free but must not dereference. Build nothing on it.
Example
#include <stdio.h>#include <stdlib.h>
int main(void){ int *scores = malloc(3 * sizeof(int));
if (scores == NULL) return 1; for (int i = 0; i < 3; i++) scores[i] = (i + 1) * 10; printf("%d %d %d\n", scores[0], scores[1], scores[2]); free(scores); return 0;}$ gcc -Wall -Wextra -g scores.c -o scores$ ./scores10 20 30Pitfalls
Not testing for
NULL: the first access crashes instead of reporting the lack of memory cleanly.Computing the size as
n * sizeof(type)without watching for overflow. Whenncomes from outside, the product can overflowsize_tand you allocate too little.callocdoes that check for you.Forgetting the matching
free: everymallocgets exactly onefree, or the memory leaks. A Valgrind report shows it.Losing the only pointer to the block (reassigning it, leaving the function without returning it): the block still exists, but nobody can free it anymore.
Cited in
- Array vs pointer in C: sizeof, char[] and char *
- free(): double free detected in tcache 2 in C
- Learn C programming in 2026: where to start
- Valgrind output explained: Invalid read, definitely lost
- implicit declaration of function in C: causes and fix
- Linked list in C: insert, delete and free without leaks
- malloc, calloc, realloc and free in C: the differences
- Pointers in C explained with memory diagrams
- Segmentation fault in C: causes and how to find the line
- Stack vs heap in C: difference, lifetime and size
- Strings in C: '\0', strlen, strcpy vs strncpy, strcmp
- Valgrind on Mac M1 and Windows: what to use in 2026