Reputation: 65
The issue I am having is with my vector going out of scope. Both the constructor and the other method are being called in the main one right after another but the problem is that after the constructor runs the vector goes out of scope. Anyone have any thoughts on how to fix this? I thought i fixed it but just made things worse.
header:
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);
};
implementation:
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;
}
The main makes a call directly from myCache(infile)
to myCache.DirectMapped(i,j)
in two consecutive lines.
Thanks for your help guys. I really appreciate it.
Upvotes: 1
Views: 77
Reputation: 66451
"Before the first line" of a function is one point where debuggers frequently aren't able to display the correct values of anything (because things haven't been set up).
It's actually more likely that the vector isn't in scope yet.
Step into the function before drawing any conclusions.
Upvotes: 1