Maybe someone can help me with this. I believe I have a memory
allocation problem. The program crashes w/ a debug error.
cpp file:
#include <cstdio>
#include <cstring>
#include <iostream> // C++ I/O
#include <iomanip> // C++ I/O Manipulators
#include <fstream> // C++ File I/O
#include <cstdlib>
#include <new>
#include "CacheSimulator.h"
using namespace std;
int main(void)
{
CacheSimulator *ptr;
//CacheSimulator temp[7];
char cache_command[10], tracefile[20];
int size, assoc, blocksize, replacement_policy, write_policy;
long int num_traces = 0;
long int i = 0;
try{
ptr = new CacheSimulator[];
} catch (bad_alloc xa){
cout << endl << "Allocation Failure!!!" << endl;
return 1;
}
if(ptr == NULL)
cout << endl << "Error" << endl;
cout << "********************Cache Sim*************************" <<
endl;
cout << endl << "To use this simulator the following input format
must"
<< endl << "be followed:" << endl;
cout << endl << "Type input as follows (all on one line & w/o "
"arrows):" << endl
<< endl << "cache_sim <SIZE<ASSOC<BLOCKSIZE<WRITE_POLICY"
"<RP<trace_file>" << endl;
cout << endl << "where SIZE = Cache size in bytes" << endl;
cout << " ASSOC = Set-associativity" << endl;
cout << " BLOCKSIZE = Block size in bytes (must be a power "
" of 2)" << endl;
cout << " WRITE POLICY = WTNA (1) or WBWA (0)" << endl;
cout << " REPLACEMENT POLICY = FIFO (0), True LRU (1), Random
(2), Optimal (3)"
<< endl;
cout << " trace_file = name of trace file" << endl;
cout << endl << endl << "Lets Begin!" << endl;
cout << endl << "Enter input here: ";
cin >cache_command >size >assoc >blocksize >write_policy
//fstream file_op(tracefile, ios::in);>replacement_policy >tracefile;
ifstream file_op(tracefile);
char file_string[20];
if(!file_op)
{
cout << endl << "File Not Found!!!" << endl;
return 1;
}
while( !file_op.eof() )
{
file_op.getline(file_string, 20);
num_traces++;
//strcpy(temp[i].file_data, file_string);
//memcpy(ptr[i].file_data, (const char *) file_string, 20);
strcpy(ptr[i].file_data, file_string);
i++;
}
/*for(int i = 0; i < num_traces; i++)
cout << endl << ptr[i].file_data << endl;*/
delete [] ptr;
cout << endl << "\t\tTotal Number of Traces: " << num_traces << endl;
file_op.close();
return 0;
}
header file
#include <iostream>
#include <cstring>
#include <iomanip>
#include <cmath>
using namespace std;
double log2(double var);
class CacheSimulator{
public:
char file_data[20];
CacheSimulator();
~CacheSimulator();
void process_data();
private:
long int num_reads;
long int num_rmisses;
long int num_writes;
long int num_wmisses;
};
CacheSimulator::CacheSimulator()
{
num_reads = 0;
num_rmisses = 0;
num_writes = 0;
num_wmisses = 0;
}
CacheSimulator::~CacheSimulator()
{
cout << endl << "\t\tSimulation Ended" << endl;
}