Question

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?

Was it helpful?

Solution

The first thing is that you should use a standard continer rather than trying a half baked manual implementation. Now to the interpretation of the code:

The code shows a common pattern. Basically you use three pointers, one to the beginning of the allocated memory, (equivalent to begin()), a pointer one beyond the last element (equivalent to end() and a third pointer one beyond the last allocated block (to avoid overflowing.

With that in mind interpreting the functions is not hard. In particular, AddOne (similar to push_back but without growing the buffer) updates the first element beyond the end, and increments the pointer to refer to one beyond the new last element.

As of EraseOne basically performs the reverse operation: it first decrements the pointer (so now the previouslast element is outside of the valid range) and then copies that element (remember that the pointer refers to one beyond the end)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top