Data analysis – memory bug c++

  c++, memory, variables

I am a data scientist, currently working on some C++ code to extract triplet particles from a rather large text file containing 2D coordinate data of particles in ~10⁵ consecutive frames. I am struggling with a strange memory error that I don’t seem to understand.

I have a vector of structs, which can be divided into snippets defined by their frame. For each frame, I build an array with unique ID’s for each individual coordinate pair, and if at any point the coordinate pair is repeated, the coordinate pair is given the old coordinate pair. This I then use later to define whether the particle triplet is indeed a trimer.
I loop over all particles and search forward for any corresponding coordinate pair. After I’m done, and no particles were found, I define this triplet to be unique and push the coordinates into a vector that corresponds to particle IDs.

The problem is: after the 18th iteration, at line trimerIDs[i][0] = particleCounter; , the variable trimerCands (my big vector array) suddenly becomes unreadable. Can this be that the vector pointer object is being overwritten? I put this vector fully on the heap, but even if I put it on stack, the error persists.

Do any of you have an idea of what I might be overlooking? Please note that I am rather new at C++, coming from other, less close to the metal, languages. While I think I understand how stack/heap allocations work, especially with respect to vectors/vector structs, I might be very wrong!

The error that Eclipse gives me in the variables tab is:

Failed to execute MI command:
-data-evaluate-expression trimerCands
Error message from debugger back end:
Cannot access memory at address 0x7fff0000000a

The function is as follows.

struct trimerCoords{
    float x1,y1,x2,y2,x3,y3;
    int frame;
    int tLength1, tLength2, tLength3;
};

void removeNonTrimers(std::vector<trimerCoords> trimerCands, int *trCandLUT){

    // trimerCands is a vector containing possible trimers, tLengthx is an attribute of the particle;
    // trCandLUT is a look up table array with indices;

    for (int currentFrame = 1; currentFrame <=framesTBA; currentFrame++){                  // for each individual frame
        int nTrimers = trCandLUT[currentFrame] - trCandLUT[currentFrame-1];                // get the number of trimers for this specific frame
        int trimerIDs[nTrimers][3] = {0};                                                  // preallocate an array for each of the inidivual particle in each triplet;
        int firstTrim = trCandLUT[currentFrame-1];                                         // first index for this particular frame
        int lastTrim = trCandLUT[currentFrame] - 1;                                        // last index for this particular frame

        bool found;
        std::vector<int> traceLengths;
        traceLengths.reserve(nTrimers*3);

        // Block of code to create a unique ID array for this particular frame
        std::vector<Particle> currentFound;
        Particle tempEntry;
        int particleCounter = 0;
        for (int i = firstTrim; i <= lastTrim; i++){                            

            // first triplet particle. In the real code, this is repeated three times, for x2/y2 and x3/y3, corresponding to the 
            tempEntry.x = trimerCands[i].x1;
            tempEntry.y = trimerCands[i].y1;
            found = false;
            for (long unsigned int j = 0; j < currentFound.size(); j++){
                if (fabs(tempEntry.x - currentFound[j].x) + fabs(tempEntry.y - currentFound[j].y) < 0.001){
                    trimerIDs[i][0] = j; found = true; break;
                }
            }
            if (found == false) {
                currentFound.push_back(tempEntry);
                traceLengths.push_back(trimerCands[i].tLength1);
                trimerIDs[i][0] = particleCounter;
                particleCounter++;
            }


        }
        // end block of create unique ID code block

        compareTrips(nTrimers, trimerIDs, traceLengths, trimerfile_out);
    }
}

If anything’s unclear, let me know!

Source: Windows Questions C++

LEAVE A COMMENT