LARA

Malloc and Free

In some programming languages, e.g. in C, it is programmer's responsibility to allocate and deallocate memory.

We illustrate this by a model where

  • ptr=malloc(N) allocates N words of memory and returns a memory address of location for this memory
  • free(ptr,N) - frees the memory cell of size N allocated at address ptr

When using free(ptr) it is programmer's responsibility to ensure that

  • ptr was previously returned as a result of malloc
  • the data stored in ptr will not be accessed any more (until it is returned as a result of calling another malloc)

Note: modern languages have garbage collection

  • automatically free storage that is not reachable in the graph of pointers