In order for any computer program, from a word processor to that hot new 3D accelerated game on the market, to use memory space in RAM, first it needs to request that space. Some higher level programming languages sometimes allow you to let the compiler (or interpreter) allocate the memory space you need as you need it, but others, lower level languages especially, force you to allocate your memory before hand. Even so, this tends to be a very simple feat, and is covered in the very first days of learning such a language. In the C programming language, to allocate space for fifty integers would be done with the following line:
int foo[50];
But what if you goofed when you initialized such space? What if, later on, you discover you actually needed 60 integers instead? What if the number of integers you need is a variable, not a constant? Well, C allows you to allocate memory on the fly, so to speak, at run time. It does this with the malloc function, which stands for Memory Allocation.


What to include to use it:

#include <stdlib.h>
The prototype:
void *malloc(size_t size);
Description:
malloc() allocates unused memory space whose size in bytes is specified by size. The type is unspecified, as this memory space can now be used for anything, given enough room.
Return Value:
Either returns a pointer to the memory address allocated, or, if something goes wrong, the null pointer. The result of this function, barring errors, can be passed to the free() function, which will return the memory space to the operating system once the program is done with it.
Errors:
malloc() will fail if size is set to zero, or if insufficient memory is available. On POSIX implementation, it will set errno to ENOMEM in the latter case.
See also:
calloc, dynamic array, free, new, realloc