0

Answered:

changing

while (tracefile->good()){
    getline(*tracefile,line);
....

to

while(getline(*tracefile, line)){
....

did the trick. Thank you, @pmr.

Original:

I made a cache simulator for my computer architecture class which is supposed to read in a trace file that contains memory instructions for my cache.

I used a test trace file with only 1000 lines when making the program, but the actual trace files are 50k+ lines. With the test trace, the program runs perfectly. With the actual trace, the program continues until it tries to use .substr() on a line, which causes an out_of_range exception and stops my program prematurely. I investigated and found that getline() is giving empty strings when the trace is too big. Remember, this does not happen on traces <= ~5000 lines.

Does anyone know why this is happening? I am using an ifstream if that matters.

EDIT: sorry here is the code. It doesn't get past the "...."

main:

cout << "Program started.\n";
string str[6];
int i = 0;
/* check for the correct number of arguments */
if(argc != 3){
    cout<<"usage: "<< argv[0] <<" <configfile> <tracefile>\n";
    exit(0);
}
string confname = argv[1];
string tracename = argv[2];
/* open the files and check if it worked */
ifstream confile;
ifstream tracefile;

confile.open (argv[1], ios::in);
if (!confile.is_open()){
    cout<<"Could not open config file.\n";
    exit(0);
}
if (confile.is_open()) cout << "Config file loaded.\n";
tracefile.open (argv[2], ios::in);
if (!tracefile.is_open()){
    cout<<"Could not open trace file.\n";
    exit(0);
}
if (tracefile.is_open()) cout << "Trace file loaded.\n";
/* read in the data from the config file */
if (confile.is_open()){
    i = 0;
    while(confile.good() && i != 6){
        getline(confile, str[i]);
        i++;
    }
}

/* create the cache simulator */
cache csim(atoi(str[0].c_str()), atoi(str[1].c_str()), 
                atoi(str[2].c_str()), atoi(str[3].c_str()), 
                atoi(str[4].c_str()), atoi(str[5].c_str()));
cout << "Simulator started.\n";

/* send the simulator the trace */
csim.trace(&tracefile);

csim.trace:

cout << "Going through trace...";
int i;
string line;
string tag, index, offset;
this->IC = 0;
/* loop until there is no more lines */

while (tracefile->good()){
    /* get the next line and increment the instruction counter */
    getline(*tracefile,line);
    this->IC++;

    /* convert hex address to a binary address */
    string address = "";
    for (i = 0; i < line.substr(4,8).length (); i++)
    {
....
4

1 回答 1

1

看起来你的一根弦可能不够长。

while (getline(*tracefile,line);){
    /* get the next line and increment the instruction counter */

    this->IC++;

    /* convert hex address to a binary address */


    //be sure to check length of string
    if (line.size() < 12)
    {
        cerr << "line is too small" << endl;
        continue;
    }

    string address = "";

    string tmp_str = line.substr(4,8);

    for (i = 0; i < tmp_str.size(); i++) //tmp_str.size() will always be 8
    {
    }
}
于 2012-10-21T23:09:12.700 回答