c++loopsvectoropenframeworks

vector subscript out of range error using three vectors, how do I know which one is iterating wrong


Hi I'm trying to implement the Diffusion Limited Aggregation algorithm in c++ inside openframeworks. I have a class where I define a two vectors, called walkers and fixed, respectively containing the dead and living/moving and not moving particles. My update function goes like this :

void DLA::update() {

// Permutation buffer
vector<int> buffer;

for (std::size_t i = 0; i != walkers.size(); ++i) // go through every main/alive particles
{
    walkers[i].update();

    for (std::size_t j = 0; j != fixed.size(); ++j) // go through every fixed/dead particles
    {
        float distance = walkers[i].position.distance(fixed[j].position); // calculate the distance between them
        if (distance < 5) // if the distance is small enough
        {
            buffer.push_back(i); // put the index of the alvie walker in buffer for deletion
        }
    }
}
// Apply buffered information
for (std::size_t i = buffer.size(); i != 0; --i)
{
    walkers[buffer[i]].randomWalk = 0; // make it dead
    fixed.push_back(walkers[buffer[i]]); // put it in the fixed/dead vector
    walkers.erase(walkers.begin() + buffer[i]); // take it out from the main alive vector
}
buffer.clear();

}

But each time I run this I get a vector subscript out of range error. Being not familiar with c++ and visual studio, and also not use to relying on frameworks I'm having a hard time reading where the errors come from, because I can't find where in the script that I'm writting the problem is originating from. The compiler sends me to another page of code that belongs to the base openframeworks class codes saying that the errors detected in there.

I have started to go into the debug mode, but from there what I see is the raw values of my RandomWalker array.enter image description here

What is wrong with my code and how can I diagnoze this clearly from the information that I am given by Visual Studio ? Thx !

edit :

fixed it without using a buffer and just running through both vectors in reverse order

for (std::size_t i = walkers.size() - 1; i != -1; --i)
{
    for (std::size_t j = fixed.size() - 1; j != -1; --j) // go through every fixed/dead particles
    {
        // Calculate Distance
        float distance = walkers[i].position.distance(fixed[j].position); 

        if (distance < (walkers[i].radius + fixed[j].radius)) // if the distance is small enough
        {
            walkers[i].randomWalk = 0; // make it dead
            fixed.push_back(walkers[i]); // put it in the fixed/dead vector
            walkers.erase(walkers.begin() + i); // take it out from the main alive vector
            break;
        }
    }
}

Solution

  • It's this loop

    // Apply buffered information
    for (std::size_t i = 0; i != buffer.size(); ++i)
    {
        walkers[i].randomWalk = 0; // make it dead
        fixed.push_back(walkers[i]); // put it in the fixed/dead vector
        walkers.erase(walkers.begin() + i); // take it out from the main alive vector
    }
    

    You are removing elements from the front of walkers as you loop over it, and are limiting it by buffer.size()

    Are you meaning to use the indexes in buffer?