The malloc() Call
This lesson teaches you the `malloc` call with the help of some examples.
We'll cover the following...
The malloc()
call is quite simple: you pass it a size asking for some room on the heap, and it either succeeds and gives you back a pointer to the newly-allocated space, or fails and returns NULL
.
man malloc
The manual page shows what you need to do to use malloc; type man malloc
at the command line in the terminal provided below and you will see:
#include <stdlib.h>
...
void *malloc(size_t size);
From this information, you can see that all you need to do is include the header file stdlib.h
to use malloc. In fact, you don’t really need to even do this, as the C library, which all C programs link with by default, has the code for malloc()
...
Access this course and 1400+ top-rated courses and projects.