OS/161 Reference Manual


malloc - allocate memory


Standard C Library (libc, -lc)


#include <stdlib.h>

void *
malloc(size_t size);


malloc allocates size bytes of memory and returns a pointer to it. The memory is not necessarily zero-filled. (To get zero-filled memory, call bzero or memset, or use calloc.)

The pointer returned must be suitably aligned for use with any data type.

When asked to allocate zero bytes, malloc may either always return NULL, or may return distinct non-null pointers that do not point to any storage.

While malloc may at its option allocate more than size bytes to fill a request, code that calls malloc may not depend on such behavior and must not perform any accesses outside of the bounds defined by size.

It is legitimate for memory returned by malloc to not actually be physically mapped until it is used. If at the time it is used, no physical memory is available and there is no space to swap something out to make room, the process may potentially receive a fatal signal or be killed. This behavior is often somewhat contentious; a full discussion of the possible alternatives and their pros and cons is well beyond the scope of this man page.

Return Values

malloc returns a pointer to the memory allocated. If memory cannot be obtained, NULL is returned.

See Also

calloc, realloc, free