LARA

Malloc and Free

Some programming languages such as 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)

References

Donald Knuth. Fundamental Algorithms, Third Edition. Addison-Wesley, 1997. ISBN 0-201-89683-4. Section 2.5: Dynamic Storage Allocation, pp.435–456.