Algorithmic Solutions > LEDA > LEDA Guide > Simple, Basic, and Advanced Data Types > Simple Data Types > Memory Management > Example

Memory Management Example

The following example shows how to use the LEDA_MEMORY macro on a pair of doubles. In the main program the class pair is compared with another class dumb_pair that does not use the LEDA_MEMORY macro.
#include <LEDA/system/memory.h>
#include <LEDA/core/list.h>

class pair {
private:
  double x; double y;               
public:
  pair(double _x=0,double _y=0):x(_x),y(_y) {} 
  pair(const pair& p):x(p.x),y(p.y) {}  
  friend ostream& operator<<(ostream& o,const pair& p) {return o;}  
  friend istream& operator>>(istream& i,const pair& p) {return i;}
  LEDA_MEMORY(pair)  //use macro LEDA_MEMORY
};

class dumb_pair {  //does not use LEDA_MEMORY
private:
  double x; double y;                
public:
  dumb_pair(double _x=0,double _y=0):x(_x),y(_y) {}
  dumb_pair(const dumb_pair& p):x(p.x),y(p.y) {} 
  friend ostream& operator<<(ostream& o,const dumb_pair& p) {return o;}  
  friend istream& operator>>(istream& i,const dumb_pair& p) {return i;}
};

int main()
{  
  int i; 
  float t=leda::used_time();
  leda::list<dumb_pair> LD;
  for (i=0;i<1000000;i++) LD.append(dumb_pair());
  LD.clear();
  t=leda::used_time(t);
  std::cout << "t(dumb_pair): " << t << std::endl;

  t=leda::used_time();
  leda::list<pair> L; 
  for (i=0;i<1000000;i++) L.append(pair());
  L.clear();
  t=leda::used_time(t);
  std::cout << "t(pair): " << t << std::endl;

  return 0;
}

Manual Entries:

Manual Page Memory Management

LEDA Item Concept




Please send any suggestions, comments or questions to leda@algorithmic-solutions.com
© Copyright 2001-2003, Algorithmic Solutions Software GmbH