简体   繁体   中英

Segmentation Fault, when assigning member variable

For some reason I am getting a segmentation fault when I try to assign member variablesto a value stored in a struct in a vector. If I comment out that assignment the segmentation fault goes away.

Where it happens is down below and is marked with the asterisks. The rest of my code can be found on a link to my github below if that helps.

void LinkedList::findFragments(){
    //Initialize variables
currentNode = head;
prevNode = head;
int fragListSize = 0;
vector<fragment> tempVec;
bool f = false;
fragment temp;
int arr[32];
//Find all the fragment groups
//Put all the isFree flags into an array
for(int i = 0; i < 32; i++){
    arr[i] = currentNode->isFree;
    currentNode = currentNode->next;
}
//Find Groups of 1's which show open fragments of memory
for (int i = 0; i < 32; i++) {
        if (!f && arr[i] == 1) {
                f = true;
                temp.begin = i;
        }

        if (f && arr[i] == 0) {
                f = false;
                temp.end = i - 1;
                temp.size = temp.end - temp.begin + 1;
                tempVec.push_back(temp);
        }

        if (f && i == 31) {
                f = false;
                temp.end = i;
                temp.size = temp.end - temp.begin + 1;
                tempVec.push_back(temp);
        }
}

//Make fragList equal to tempVec
fragList = tempVec;

//Sorting the fragments by size
fragListSize = fragList.size();
for(int j = 0; j < fragListSize; j++){
    for(int i = 0; i < fragListSize- j - 1; i++){
        if(fragList[i].size < fragList[i + 1].size){
            iter_swap(fragList.begin() + i, fragList.begin() + i + 1);
        }
    }
}
cout << "test 3" <<endl;
int max, min;

//Find the min and max sizes of the fragments
max =0;
for(int i = 0; i < fragListSize;i++)
{
    if(fragList[i].size > fragList[max].size)
    {
        max = i;
    }
}
min =0;
for(int i = 0; i < fragListSize;i++)
{

    if(fragList[i].size < fragList[min].size)
    {
        min = i;
    }
}

    for(int i =0; i< fragListSize; i++){
        cout << "Begin index: " << fragList[i].begin << " End index: "
                << fragList[i].end<< "Frag Size"<< fragList[i].size << endl;
    }

//Set largest and smallest Fragment size
//********************Segmentation Fault********************
largestFragment = fragList[max].size;
smallestFragment = fragList[min].size;
//Set the position of the smallest and largest fragments
largestFragmentPos = max;
smallestFragmentPos = min;

}

Project Code on GitHub

I found the bug. I was trying to get a value from an element from fragList before anything was pushed to fragList. I just used an if statement to make sure it was not empty. Sorry I'm just starting out with programming, but I really appreciate the feedback and help I get here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM