所以我遇到了一個結構向量超出範圍的問題。我不知道爲什麼。我之前使用過載體,但由於某些原因,這不起作用。我看了幾個論壇,並嘗試了幾個不同的東西來解決這個問題。矢量超出範圍
構造函數和DirectMap方法都在main中調用。我想讓這兩個方法都保持在範圍內。這可能嗎?非常感謝幫助人!
繼承人如何,我宣佈它在headerfile:
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;
contents->dirtyBit = false;
contents->statusBit = 0;
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 irrelevent stuff after this
}
再次感謝您抽出寶貴時間來幫助我。
永遠不會爲'contents'和push_back分配內存給dataBase? – billz
你在*未定義的行爲*土地:你需要爲'countents'分配內存(並釋放它)。 – Bathsheba
你的問題(也就是代碼)是有道理的......首先你有一個指針向量,但是你沒有實例化結構,你聲明瞭一個指針,然後嘗試設置一些成員,然後將這個指針放入向量中...你真的需要檢查你的代碼... – Nim