For循环中的堆栈溢出和分段故障

时间:2013-03-24 11:33:06

标签: c++ stack-overflow

这里我的代码和我不知道这个错误的原因是什么。

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>

using namespace std;

vector<vector<int>> readFile(char* fileName)
{
    vector < vector < int > > info;
    ifstream file(fileName);
    string line;
    while ( getline(file, line) )
    {
        vector < int > data;
        double value;
        istringstream iss(line);
        while (iss >> value)
        {
            data.push_back(value);
        }
        info.push_back(data);
    }
    return info;
}

void print2DVector(vector <vector <int> > input)
{
    for ( vector < vector < int > > :: size_type i = 0, size = input.size(); i < size; ++i)
    {
        for ( vector < int > :: size_type j = 0, length = input[i].size(); j < length; ++j)
        {
            cout << input[i][j] << " ";
        }
        cout << endl;
    }
}

vector <vector <int> > inverseVector(vector <vector <int> > input)
{
    int* arr;
    arr = new int[input[0].size()];
    vector <int>  data;
    vector <vector <int> > output;
    for ( vector < vector < int > > :: size_type i = 0, size = input.size(); i < size; ++i)
    {
        for ( vector < int > :: size_type j = 0, length = input[i].size(); j < length; ++j)
        {
            arr[input[i][j]] = j;
        }
        for ( vector < int > :: size_type j = 0, length = input[i].size(); j < length; ++j)
        {
            data.push_back(arr[j]);
        }

        output.push_back(data);
        data.clear();
    }

    return output;
}

vector<vector <int>> initializeEmptyHospitals(int quota, int count)
{
    vector<vector <int>> output;
    vector<int> data;
    for(int j=0; j<quota;j++)
        data.push_back(-1);
    for(int i=0; i<count; i++)
        output.push_back(data);

    data.clear();
    return output;
}

bool isEmpty(vector <int> hospital)
{
    for(int i=0;i<hospital.size();i++)
        if(hospital[i]==-1)
            return true;

    return false;
}

void reArrangeVector(vector <int> &hospital)
{
    int temp;
    for (int i = 1; i < hospital.size(); i++)
    {
        for (int j = 0; j < hospital.size()- i; j++)
        {
            if(hospital[j]>=0 && hospital[j+1]>=0)
            if(hospital[j] > hospital[j+1])
            {
                temp = hospital[j+1];
                hospital[j+1] = hospital[j];
                hospital[j] = temp;
            }
        }
    }
}

void assignResident( vector <int> residentChoices ,vector < vector <int> > &hospitalsWithQuotas, int residentNumber, vector < vector <int> > inverseHospitals)
{
    for(int i=0;i<residentChoices.size();i++)
    {
        if(isEmpty(hospitalsWithQuotas[residentChoices[i]]))
        {
            for(int j=0; j<hospitalsWithQuotas[residentChoices[i]].size(); j++)
                if(hospitalsWithQuotas[residentChoices[i]][j] == -1)
                {
                    hospitalsWithQuotas[residentChoices[i]][j]=residentNumber;
                    reArrangeVector(hospitalsWithQuotas[residentChoices[i]]);
                    return;
                }   
        }
        else
        {
            for(int k=0; k<hospitalsWithQuotas[residentChoices[i]].size(); k++)
            {
                if(inverseHospitals[residentChoices[i]][residentNumber] < hospitalsWithQuotas[residentChoices[i]][0])
                {
                    int temp = hospitalsWithQuotas[residentChoices[i]][0];
                    hospitalsWithQuotas[residentChoices[i]][0] = residentNumber;
                    assignResident(residentChoices, hospitalsWithQuotas, temp, inverseHospitals);
                    reArrangeVector(hospitalsWithQuotas[residentChoices[i]]);
                    return;
                }
                //if(k==hospitalsWithQuotas[residentChoices[i]].size()-1)
            }
        }
    }
}

int main(int argc, char* argv[])
{
    vector < vector <int> > hospitalChoices,residentChoices,inverseHospitals,hospitalsWithQuotas;
    int quota = 3;
    hospitalChoices = readFile("data1_h1.txt");
    residentChoices = readFile("data1_r1.txt");
    hospitalsWithQuotas = initializeEmptyHospitals(quota, hospitalChoices.size());  
    inverseHospitals = inverseVector(hospitalChoices);

    for(int i=0; i<15; i++)
        assignResident(residentChoices[i],hospitalsWithQuotas,i,inverseHospitals);

    print2DVector(hospitalsWithQuotas);

    system("pause");
    return 0;
}

在下面的for循环中,当我写i&lt; 12时它可以工作但是对于i&lt; 15它会抛出一个堆栈溢出异常。

for(int i=0; i<15; i++)
    assignResident(residentChoices[i],hospitalsWithQuotas,i,inverseHospitals);

整个错误文本在VS2012中:

Unhandled exception at 0x00CB33D9 in BLG372E HW1.exe: 0xC00000FD: Stack overflow (parameters: 0x00000001, 0x00E72F50).

1 个答案:

答案 0 :(得分:2)

程序中的一些错误:

  1. 在readFile函数中,值未初始化,您正在比较它。在比较之前初始化它。
  2. 在readFile函数中,你正在使用vector而你正在推送字符,这是错误的。
  3. 你得到Stackoverflow错误可能是因为一些无休止的递归。

    建议:请在将代码发布到任何论坛之前格式化代码,其他人可以轻松调试代码。