Skip to content
Tutorials Center
My learning

Topics

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.

C
#include <stdlib.h>

void *malloc(size_t size);

Try it

C An array whose size is decided at run time

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.

C Allocating a struct and a string

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

C
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, not sizeof(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 *p wraps around for a huge n, giving a small block and then a buffer overflow. Use calloc(n, size), which checks for it, or check n > SIZE_MAX / sizeof *p yourself. C23 also offers ckd_mul in <stdckdint.h>.
  • Strings need strlen(s) + 1. Forgetting the terminator is the single most common off-by-one with malloc.
  • Every malloc needs exactly one free. 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: malloc and free are thread-safe in glibc.