The malloc(3) API

Perhaps one of the most common APIs used by application developers is the renowned malloc(3).

The foo(3) syntax indicates that the foo function is in section 3 of the manual (the man pages) a library API, not a system call. We recommend you develop the habit of reading the man pages. The man pages are available online, and you can find them at https://linux.die.net/man/.

We use malloc(3) to dynamically allocate a chunk of memory at runtime. This is as opposed to static—or compile-time – memory-allocation where we make a statement, such as:

char buf[256];

In the preceding case, the memory has been statically allocated (at compile-time).

So, how exactly do you use malloc(3)? Let's check out its signature:

#include <stdlib.h> ...

Get Hands-On System Programming with Linux now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.