Types of memory

Stack memory / automatic memory: allocations and deallocations are done implicitly(隐式地) by the compiler.

Heap memory: allocations and deallocations are done explicitly by programmer.

The malloc() call

Dependency: stdlib.h

Usage: void *malloc(size_t size);

size: the number of bytes needed, usually calculated via sizeof().

Returns the pointer to allocated memory block.

Warning: sometimes if you use sizeof(some_pointer), it returns the size of the pointer instead of the size of the content.

The free() call

Used for freeing heap memory no longer in use.

Underlying OS support

malloc() is based on brk() system call. It changes the location of the end of the heap.

mmap() creates an anonymous mem region in swap space.