
Untitled
By: a guest on
Apr 29th, 2012 | syntax:
None | size: 0.67 KB | hits: 16 | expires: Never
Memory management trickery with arrays
/// the first particle in the linked list
Particle* start=0;
/// the next free particle
Particle* last=0;
/// the end of the memory allocation
Particle* end=0;
void SetSize(unsigned int size) {
// delete any previous data
delete [] start;
// allocate new particles
last = start = new Particle[size];
// set end
end = start+size;
}
void AddOne() {
// if we have no more memory left for any particles, ignore
// the request to creat one.
if (!IsFull()) {
*last = Particle();
++last;
}
}
void EraseOne(Particle* p) {
if (!IsEmpty()) {
*p = *(--last);
}
}