Guest User

Untitled

a guest
Apr 29th, 2012
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment