堆栈超出范围。也许没有正确地称它?

时间:2013-12-12 11:47:07

标签: c++ vector scope polymorphism

我遇到的问题是我的矢量超出了范围。构造函数和其他方法都在主要的一个接一个地被调用,但问题是在构造函数运行后,向量超出了作用域。任何人都有任何想法如何解决这个问题?我以为我修好了但是让事情变得更糟。

头:

struct input
{
    bool dirtyBit;
    int statusBit; //0 not in cache, 1 in cache, 2 in 2nd cache
    bool writeStatus; //write = 1 read = 0
    int address;
    int indivBlockIndex;
    int indivBlockOffset;
};

class Cache
{

public:

    vector<input > dataBase;
    Cache(string);
    ~Cache();

    void DirectMapped(int, int);
};

实施:

Cache::Cache(string infile)
{
    ifstream in(infile);

    string readWriteStatus;
    int Addr;
    while (in >> readWriteStatus >> hex >> Addr)
    {
        input contents;
        //contents = new input;
        if (readWriteStatus == "read")
            contents.writeStatus = true;
        else if (readWriteStatus == "write")
            contents.writeStatus = false;
        contents.address = Addr;
        contents.dirtyBit = false;
        contents.statusBit = 0;
        contents.indivBlockIndex = -1;
        contents.indivBlockOffset = -1;
        dataBase.push_back(contents);

    }
}

Cache::~Cache(){}

void Cache::DirectMapped(int cacheSize, int blockSize)
{
    //initial stats needed
    int blockCount = cacheSize/blockSize; //number of total blocks
    //clear out the cache
    for (int i = 0; i < dataBase.size(); i++)
        dataBase[i].statusBit = 0;
        //other stuff not related to question
}

主:

int main(int argc, char *argv[])
{


    //string in = argv[1]; 
    string inputfile = "C:/Users/Christopher/Downloads/testprac";
    string infile = inputfile.append(".trace");
    Cache myCache(infile);

    // Parse Command Line Argument
//    if(argc != 2)
//        cout << "ERROR: Improper Number of Arguments" << endl;
//    else
//    {
    int i = 1024, j = 8;
    myCache.DirectMapped(i,j);

        system ( "pause");
    return 0;
}

主要在两个连续的行中直接从myCache(infile)拨打myCache.DirectMapped(i,j)

感谢您的帮助。对此,我真的非常感激。

1 个答案:

答案 0 :(得分:1)

函数的“第一行之前”是调试者经常无法显示任何内容的正确值的一点(因为事情尚未设置)。

实际上,矢量在范围内的概率不太可能。

在得出任何结论之前进入该功能。