堆栈超出范围.也许没有正确地称呼它

Stack going out of scope. Maybe not calling it correctly?

本文关键字:正确地 范围 也许 堆栈      更新时间:2023-10-16

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

标题:

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
}

main:

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;
}

main在两条连续的线路中直接从myCache(infile)myCache.DirectMapped(i,j)进行调用。

谢谢你们的帮助。我真的很感激。

函数的"第一行之前"是调试器经常无法显示任何正确值的地方(因为事情还没有设置好)。

实际上,向量不在范围内的可能性更大。

在得出任何结论之前,先进入函数。