Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 29th, 2012  |  syntax: None  |  size: 0.67 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Memory management trickery with arrays
  2. /// the first particle in the linked list
  3.     Particle* start=0;
  4.  
  5. /// the next free particle
  6. Particle* last=0;
  7.  
  8. /// the end of the memory allocation
  9. Particle* end=0;
  10.  
  11. void SetSize(unsigned int size) {
  12.  
  13.     // delete any previous data
  14.     delete [] start;
  15.  
  16.     // allocate new particles
  17.     last = start = new Particle[size];
  18.  
  19.     // set end
  20.     end = start+size;
  21. }
  22.  
  23. void AddOne() {
  24.  
  25.     // if we have no more memory left for any particles, ignore
  26.     // the request to creat one.
  27.     if (!IsFull()) {
  28.         *last = Particle();
  29.         ++last;
  30.     }
  31.  
  32. }
  33.  
  34. void EraseOne(Particle* p) {
  35.  
  36.     if (!IsEmpty()) {
  37.         *p = *(--last);
  38.     }
  39.  
  40. }