This is my first time using stackoverflow for a question, I have read a few answers previously which have helped me in some cases so I thought I'd register seeing as I can't find the specific answer I'm looking for. Recently I made a pretty simple particles system that spews out a couple 100 to 1000s of particles just to see if I could do it, before I even started this I made a simple Linked List which uses templates so I can use it in other programs if I so choose.
Recently, after seeing a colleague of mine playing about with a particle system he found I decided to revisit my project to improve upon it. I scoured the internet to find a little example which apparently ditches the idea of using a Linked List and instead uses and array and three pointers to manage the particles. I understand most of the concept but for some reason one thing escapes me.
/// 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);
}
}
From what I understand from the code above the three pointers act as simple pointers to the elements in the array. The start pointer remains at zero and the end pointer remains at the end of the array while last starts from the same position as the start pointer and moves along like an index counter until it gets to the end.
What I'm unsure of is the erasing bit, I assume from the code above that 'p' is becoming not the particle the last pointer is pointing to but the one before last. Unfortunately I have no idea why it is done this way as surely the one before last is a perfectly alive particle but wouldn't this make two instances of the same particle?