Zalnur 434
Zalnur 434

Reputation: 37

Segmentation fault: 11 c++ when using vector

i am trying to write a program for my university project the program is supposed to do first come first serve scheduling i have thought a lot about this function but i don't know how to make it work, i always get Segmentation fault: 11, i also tried to use temp.at(j) but it gave me Segmentation fault: 6, and i tried to minimise the vector so it would be in-bound by declaring the vectors outside the function, then use temp.size() instead of Processes but it also did't work.

void FCFS(Process ProcessDetails[], int Processes)
{
    vector<int> temp;
    vector<int> temp1;
    int first = 0; //first value to compare with.
    for(int j = 0; j < Processes; j++){ // to make sure that it passes through all elements.
        for(int i = 0; i < Processes; i++){ // pass each PID and Burst time to vector temp and temp1.
            if(ProcessDetails[i].ArrivalTime == first){
                temp.operator[](j) = ProcessDetails[i].PID;
                temp1.operator[](j) = ProcessDetails[i].BurstTime;
            }
        }
        first++;// increase first value to declare the round is finished and start a new one.
    }
    for(int i = 0; i < Processes; i++){ // pass the sorted vector values back to the arrays.
        ProcessDetails[i].PID = temp.operator[](i);
        ProcessDetails[i].BurstTime = temp1.operator[](i);
    }
}

the program works fine until it reaches this function, please help.

Upvotes: 2

Views: 2657

Answers (3)

Peter
Peter

Reputation: 36607

The behaviour of a vector's operator[]() is undefined if it is used to access elements that do not exist.

Since you have used default-constructed vectors, their size is zero - so they have no elements to access.

If you use the .at() member function, it will check the index and throw an exception (of type std::out_of_range, which is declared in the standard header <stdexcept>) when indices are invalid. You can confirm that by wrapping the code in an appropriate try/catch block.

To eliminate the problem, you need to reize the vector (e.g. add elements to it using push_back(), resize it using resize(), etc) before using operator[](). And ensure the index is valid, since operator[]() does not resize a std::vector.

Also, temp[j] is equivalent to temp.operator[](j). For types that supply an operator[]() function, the compiler handles turning expressions like temp[j] into a call of temp.operator[](j).

Upvotes: 2

Hariom Singh
Hariom Singh

Reputation: 3632

you will have to change your vector assignment to

       if(ProcessDetails[i].ArrivalTime == first){
            temp.push_back(ProcessDetails[i].PID);
            temp1.push_back(ProcessDetails[i].BurstTime);
        }

Upvotes: 0

user673679
user673679

Reputation: 1366

Your vectors have no elements.

Using the vector operator[] will therefore fail.

Use push_back, emplace, resize, or some other function to add elements to the vectors.

Upvotes: 2

Related Questions