Skip to content

R) Memory Management

kalinosia edited this page Mar 13, 2021 · 1 revision

Stack and heap

’new’ operator ??????????

Problems related with dynamic memory allocation

  • accessing out-of-bounds memory
  • dangling pointer
  • double deleting
  • null pointer dereference
  • freeing memory blocks that were not dynamically allocated
  • attempting to free a portion of a dynamic block
  • memory leak

Stack vs Heap

  • Stack
    • very fast access
    • space is managed efficiently by CPU,
    • memory will not become fragmented
    • limit on stack size (OS-dependent)
  • Heap
    • no limit on memory size
    • slower access
    • memory may become fragmented
    • you must manage memory (you’re in charge of allocating and freeingvariables)

Memory management

Implicit (automatic) memory management:

  • garbage collector
    • mark and sweep
    • reference counting

In C++ there is no automatic memory management.

Explicit memory management:

  • RAII
  • smart pointers (which implements RAII)

Clone this wiki locally