i'm trying to input data into a vector of vectors, but the program crashes.
there is no compile errors, but it is obvious that my logic is not valid.
it started to crash after i inserted the if statement.
Code:
#include <vector>
#include <iostream>
#include <fstream>
#include <conio.h>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
vector< vector< unsigned long > > vecMemoryCache;
long lgNumberOfBlocks=0;
long lgBlocksPerSet=0;
long lgByteAddress=0;
long lgByteAddressConverted=0;
long lgBlockSize=0;
long lgAssociativity=0;
long lgCacheSize=0;
long lgHit=0;
long lgMiss=0;
unsigned long unlgTotalLoads=0;
string stFileName(argv[2]);
string stProgramName(argv[0]);
if (argc != 9)
{
cout<<"!!Invalid Number of arguments.\nUsage: "<< argv[0] <<" -t <file_name> -s <cache_size> -b <block_size> -a <associativity>!!\n";
exit(1);
}
ifstream inTraceFile(argv[2], ios::in);
if(!inTraceFile.is_open())
{
cout<<"!!Unable to open file "<<argv[2]<<"!!\n";
exit(2);
}
lgBlockSize = atoi(argv[6]);
lgCacheSize = atoi(argv[4]);
lgAssociativity = atoi(argv[8]);
lgNumberOfBlocks = lgCacheSize/lgBlockSize;
lgBlocksPerSet = lgNumberOfBlocks/lgAssociativity;
/*
Address Conversion
Exact Details:
Each line represents a byte adddress referenced by an executed load instruction. To help compress
the traces, instead of storing the absolute memory byte address, we store the difference from the
last address in the trace.
I construct the Byte Address, Block Address, and Cache Address.
*/
if (lgAssociativity == 1)
{
while(inTraceFile>>lgByteAddress)
{
lgByteAddressConverted=lgByteAddressConverted+lgByteAddress;
cout<<"Byte Address: "<<lgByteAddressConverted<<endl;
cout<<"Block Address: "<<lgByteAddressConverted/lgBlockSize<<endl;
cout<<"Cache Address: "<<(lgByteAddressConverted/lgBlockSize)%lgNumberOfBlocks<<endl;
if(vecMemoryCache[0][(lgByteAddressConverted/lgBlockSize)%lgNumberOfBlocks]==lgByteAddressConverted/lgBlockSize)
{
lgHit++;
}
else
{
lgMiss++;
vecMemoryCache[0][(lgByteAddressConverted/lgBlockSize)%lgNumberOfBlocks]=lgByteAddressConverted/lgBlockSize;
}
unlgTotalLoads++;
if(getch()==113)
break;
}
}
cout<<"Program Name: "<<stProgramName<<endl;
cout<<"Trace File Name: "<<stFileName<<endl;
cout<<"Cache Size: "<<lgCacheSize<<endl;
cout<<"Block Size: "<<lgBlockSize<<endl;
cout<<"Associativity: "<<lgAssociativity <<endl;
cout<<"Total Loads: "<<unlgTotalLoads<<endl;
cout<<"Total Hits: "<<lgHit<<endl;
cout<<"Total Misses: "<<lgMiss<<endl;
return 0;
}