Malloc

Malloc allocates memory and returns the address of the first byte of that memory. It is on the onus of the caller to remember how much memory was requested.

int* count = malloc(sizeof(int));         // for an int
char* message = malloc(50 * sizeof(char)) // for a message that will be maximum 50 chars

Memory layout

Memory layout of a running program is comprised of different areas.

  1. Text - the area which holds the running program
  2. Initialised data
  3. Uninitialised data
  4. Heap
  5. Stack
  6. Environment variables