分段错误:11 c++ 使用向量时 [英] Segmentation fault: 11 c++ when using vector

查看:33
本文介绍了分段错误:11 c++ 使用向量时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的大学项目编写一个程序,该程序应该执行先到先得调度我对这个功能想了很多,但我不知道如何实现工作,我总是得到分段错误:11,我也尝试使用 temp.at(j) 但它给了我 分段错误:6,我试图通过在函数外部声明向量来最小化向量,然后使用 temp.size() 而不是 Processes 但它也没有用.

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.

推荐答案

如果向量的 operator[]() 用于访问不存在的元素,则其行为未定义.

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.

如果使用 .at() 成员函数,它将检查索引并抛出异常(类型为 std::out_of_range,在索引无效时的标准标头 ).您可以通过将代码包装在适当的 try/catch 块中来确认这一点.

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.

为了消除这个问题,你需要在使用之前重新调整向量(例如使用push_back()向它添加元素,使用resize()调整它的大小,等等)operator[]().并确保索引有效,因为 operator[]() 不会调整 std::vector 的大小.

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.

另外,temp[j] 等价于 temp.operator[](j).对于提供 operator[]() 函数的类型,编译器将像 temp[j] 这样的表达式转换为 temp.operator[](j).

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).

这篇关于分段错误:11 c++ 使用向量时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆