next up previous contents index
Next: Memory Allocator ( leda_allocator Up: Simple Data Types and Previous: Dynamic Random Variates (   Contents   Index


Memory Management

LEDA offers an efficient memory management system that is used internally for all node, edge and item types. This system can easily be customized for user defined classes by the ``LEDA_MEMORY" macro. You simply have to add the macro call ``LEDA_MEMORY(T)" to the declaration of a class T. This redefines new and delete operators for type T, such that they allocate and deallocate memory using LEDA's internal memory manager.

struct pair {
  double x;
  double y;

  pair() { x = y = 0; }
  pair(const pair& p) { x = p.x; y = p.y; }

  friend ostream& operator<<(ostream&, const pair&) { ... }
  friend istream& operator>>(istream&, pair&)       { ... }
  friend int compare(const pair& p, const pair& q)  { ... }

  LEDA_MEMORY(pair)

};

dictionary<pair,int> D;

The LEDA memory manager only frees memory at its time of destruction (program end or unload of library) as this allows for much faster memory allocation requests. As a result, memory that was deallocated by a call to the redefined delete operator still resides in the LEDA memory management system and is not returned to the system memory manager. This might lead to memory shortages. To avoid those shortages, it is possible to return unused memory of LEDA's memory management system to the system memory manager by calling
leda::std_memory_mgr.clear();