C Reference ยท stdlib.h
C malloc() function
Reference for C malloc(): allocate uninitialized heap memory, compute sizes safely, check for NULL, and avoid leaks and overflow in the size calculation.
malloc() requests a block of memory from the heap and returns a pointer to its first byte. The memory stays valid until you release it with free(), so it can outlive the function that allocated it and can have a size chosen at run time.
The block is uninitialized: it contains whatever bytes happened to be there. If you need zeros, use calloc(). The returned pointer is suitably aligned for any ordinary type, so you can store int, double or a struct in it directly.
#include <stdlib.h> void *malloc(size_t size);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
void *malloc(size_t size);
| Name | Description |
|---|---|
size |
Number of bytes to allocate. |
Returns: a pointer to the new block, or NULL if the memory could not be allocated. For size == 0 the result is implementation defined: either NULL or a unique pointer that must not be dereferenced (glibc returns a small valid block). In C23 malloc(0) is still implementation defined.
Errors / undefined behavior: reading the memory before writing it gives indeterminate values (undefined behavior for most types). Writing past size bytes, using the block after free(), freeing it twice, or passing a pointer not from malloc to free() are all undefined. See Memory errors.
Usage notes
- Always check for
NULL. On modern desktop systems allocation rarely fails, but servers with limits and embedded devices do; a missing check turns into a crash at a confusing place. - Write
sizeof *ptr, notsizeof(type).p = malloc(n * sizeof *p);stays correct if you later change the pointer's type. - No cast needed in C.
void *converts implicitly. A cast hides a missing#include <stdlib.h>in old compilers; in C++ the cast is required. - Beware of overflow in the size.
n * sizeof *pwraps around for a hugen, giving a small block and then a buffer overflow. Usecalloc(n, size), which checks for it, or checkn > SIZE_MAX / sizeof *pyourself. C23 also offersckd_mulin<stdckdint.h>. - Strings need
strlen(s) + 1. Forgetting the terminator is the single most common off-by-one withmalloc. - Every
mallocneeds exactly onefree. Decide who owns the block; see Memory ownership patterns. - Cost. Allocation takes noticeable time (hundreds of nanoseconds) and fragments memory; for many small objects allocate in bulk or use an arena.
- Contents are not zero even if they look zero in a small test; fresh pages from the operating system happen to be zeroed, recycled blocks are not.
- Thread safety:
mallocandfreeare thread-safe in glibc.
Related
calloc,realloc,free,aligned_alloc, Memory errors- Lessons: Dynamic memory, Memory errors, Dynamic arrays