gpt4 book ai didi

c++ - 堆栈超出范围。也许没有正确调用它?

转载 作者:太空宇宙 更新时间:2023-11-04 11:43:27 25 4
gpt4 key购买 nike

我遇到的问题是我的 vector 超出了范围。构造函数和另一个方法都在主函数中被调用,但问题是在构造函数运行后, vector 超出了范围。有人对如何解决这个问题有任何想法吗?我以为我修复了它,但只是让事情变得更糟。

标题:

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

main 在连续两行中直接从 myCache(infile) 调用 myCache.DirectMapped(i,j)

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

最佳答案

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

实际上更有可能的是 vector 尚未范围内。

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

关于c++ - 堆栈超出范围。也许没有正确调用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20542664/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com