我的C ++模拟器崩溃了终端

时间:2014-04-11 21:01:49

标签: c++ windows crash

我的代码每次执行时都会崩溃终端。我已经尝试使用Geany和Visual C ++进行调试,并且在通过cmd提示时它也会崩溃。

我使用调试器摆脱了所有错误,但它没有帮助。我应该创建一个缓存模拟器,并使用随机数查找命中和未命中,这是我的代码:

#include <iostream>
#include <cmath>
#include <stdlib.h>
#include <ctime>
#include <iomanip>
#include <fstream>

using namespace std;

const int r = 8192;
const int c = 3;
const int r1 = 262144;
int i;
int j;
int k;

int max;
int min;
int numberW0;
int index_max;
int index_min;
int numberX0;
int tag_max;
int tag_min;
int numberY0;
unsigned int data_max;
int data_min;
int numberZ0;

int numberW1;
int index_max1;
int index_min1;
int numberX1;
int tag_max1;
int tag_min1;
int numberY1;
unsigned int data_max1;
int data_min1;
int numberZ1;

int numberW2;
int index_max2;
int index_min2;
int numberX2;
int tag_max2;
int tag_min2;
int numberY2;
unsigned int data_max2;
int data_min2;
int numberZ2;

int numberW3;
int index_max3;
int index_min3;
int numberX3;
int tag_max3;
int tag_min3;
int numberY3;
unsigned int data_max3;
int data_min3;
int numberZ3;

int numberW4;
int index_max4;
int index_min4;
int numberX4;
int tag_max4;
int tag_min4;
int numberY4;
unsigned int data_max4;
int data_min4;
int numberZ4;

double new_valid = 0;
double new_tag = -9999999999;
double new_data = -9999999999;

double tag_value;
int s;
int t;
double block0;
double block1;
double block2;
double block3;
int v;
double levBlock;

fstream armRes;

int main()
{
    double way0 [r][c];  //declaration of the arrays
    double way1 [r][c];
    double way2 [r][c];
    double way3 [r][c];

    double lev2[r1][c];

    for (i = 0; i < r; i++) // initialization of the arrays
    {
        way0[i][0] = new_valid;
        way0[i][1] = new_tag;
        way0[i][2] = new_data;

        way1[i][0] = new_valid;
        way1[i][1] = new_tag;
        way1[i][2] = new_data;

        way2[i][0] = new_valid;
        way2[i][1] = new_tag;
        way2[i][2] = new_data;

        way3[i][0] = new_valid;
        way3[i][1] = new_tag;
        way3[i][2] = new_data;  
    }

    for (j = 0; j < r1; j++)
    {
        lev2[j][0] = new_valid;
        lev2[j][1] = new_tag;
        lev2[j][2] = new_data;
    }


    srand (time(0));  // random number generator seed


    int max = r;
    int min = 0;
    int max1 = r1;
    int min1 = 0;
    int numberW0 = (rand()%max+min+1); // number of entires to populate way0

    for (k = 0; k <= numberW0; k++)
    {
        int index_max = 8192;
        int index_min = 0;

        numberX0 = rand()%index_max+index_min+1; // random index to use

        int tag_max = 32768;
        int tag_min = 0;
        numberY0 = rand()%tag_max+tag_min+1; // random tag for the index

        unsigned int data_max = 429496729; // random data for the index
        int data_min = 0;
        numberZ0 = rand()%data_max+data_min+1;

        way0 [numberX0][0] = 1;
        way0 [numberX0][1] = numberY0;
        way0 [numberX0][2] = numberZ0;
    }

    int numberW1 = (rand()%max+min+1); // number of entires to populate way1

    for (k = 0; k <= numberW1; k++)
    {
        int index_max1 = 8192;
        int index_min1 = 0;

        numberX1 = rand()%index_max1+index_min1+1; // random index to use

        int tag_max1 = 32768;
        int tag_min1 = 0;
        numberY1 = rand()%tag_max1+tag_min1+1; // random tag for the index

        unsigned int data_max1 = 429496729; // random data for the index
        int data_min1 = 0;
        numberZ1 = rand()%data_max1+data_min1+1;

        way1 [numberX1][0] = 1;
        way1 [numberX1][1] = numberY1;
        way1 [numberX1][2] = numberZ1;
    }

    int numberW2 = (rand()%max+min+1); // number of entires to populate way2

    for (k = 0; k <= numberW2; k++)
    {
        int index_max2 = 8192;
        int index_min2 = 0;

        numberX2 = rand()%index_max2+index_min2+1; // random index to use

        int tag_max2 = 32768;
        int tag_min2 = 0;
        numberY2 = rand()%tag_max2+tag_min2+1; // random tag for the index

        unsigned int data_max2 = 429496729; // random data for the index
        int data_min2 = 0;
        numberZ2 = rand()%data_max2+data_min2+1;

        way2 [numberX2][0] = 1;
        way2 [numberX2][1] = numberY2;
        way2 [numberX2][2] = numberZ2;
    }

    int numberW3 = (rand()%max+min+1); // number of entires to populate way3

    for (k = 0; k <= numberW3; k++)
    {
        int index_max3 = 8192;
        int index_min3 = 0;

        numberX3 = rand()%index_max3+index_min3+1; // random index to use

        int tag_max3 = 32768;
        int tag_min3 = 0;
        numberY3 = rand()%tag_max3+tag_min3+1; // random tag for the index

        unsigned int data_max3 = 429496729; // random data for the index
        int data_min3 = 0;
        numberZ3 = rand()%data_max3+data_min3+1;

        way3 [numberX3][0] = 1;
        way3 [numberX3][1] = numberY3;
        way3 [numberX3][2] = numberZ3;
    }

    int numberW4 = (rand()%max1+min1+1); // number of entires to populate level 2

    for (k = 0; k <= numberW4; k++)
    {
        int index_max4 = r1;
        int index_min4 = 0;

        numberX4 = rand()%index_max4+index_min4+1; // random index to use

        int tag_max4 = 32768;
        int tag_min4 = 0;
        numberY4 = rand()%tag_max4+tag_min4+1; // random tag for the index

        unsigned int data_max4 = 429496729; // random data for the index
        int data_min4 = 0;
        numberZ4 = rand()%data_max4+data_min4+1;

        lev2 [numberX4][0] = 1;
        lev2 [numberX4][1] = numberY4;
        lev2 [numberX4][2] = numberZ4;
    }

    armRes.open("C:\\Users\\Max\\Documents\\CSIT4\\Project\\Practice\\ARMresults.txt", ios::out | ios::app );

    for (t = 0; t<= 15000; t++)
    {
        int tag_value = rand()%32768+0+1;

        for (s = 0; s <= r; s++)
        {
            block0 = way0 [s][1];
            block1 = way1 [s][1];
            block2 = way2 [s][1];
            block3 = way3 [s][1];

            if (tag_value == block0)
            {
                armRes << "L1 Hit (Way 0) -- AT INDEX: " << s << endl;
            }

            else if (tag_value == block1)
            {
                armRes << "L1 Hit (Way1) -- AT INDEX: " << s << endl;
            }   

            else if (tag_value == block2)
            {
                armRes << "L1 Hit (Way2) -- AT INDEX: " << s << endl;
            }   
            else if (tag_value == block3)
            {
                armRes << "L1 Hit (Way3) -- AT INDEX: " << s << endl;
            }

            else 
            {
                for (v = 0; v <= r1; v++)
                {
                    double levBlock = lev2 [v][1];

                    if (tag_value == levBlock)
                    {
                        armRes << "L2 Hit -- AT INDEX: " << v << endl;
                    }

                    else

                    {
                        armRes << "Cache Miss For Tag: " << tag_value << endl;
                    }
                }
            }   
        }
    }

    armRes.close();

    return 0;

}

任何帮助都会非常感激,因为我不再有任何学术帮助

1 个答案:

答案 0 :(得分:2)

立即出现一个问题:你很可能会在main()中使用那些数组所占用的内存来清理堆栈。由于您使用的是Visual C ++,我可以假设以下内容:

double way0 [r][c];  //declaration of the arrays
double way1 [r][c];
double way2 [r][c];
double way3 [r][c];
double lev2[r1][c];

鉴于此:

const int r = 8192;
const int c = 3;
const int r1 = 262144;

如果sizeof(double)== 8,那么保存这些数组所需的堆栈超过7,000,000字节。默认堆栈是(我相信)1兆字节(可能是2兆字节,无法从头顶回忆),但远不及接近7兆字节。

因此,您需要使用动态分配来创建这些数组。最简单的方法是相应地使用std :: vector和size。

#include <vector>
//...
typedef std::vector<double> Double1D;
typedef std::vector<Double1D> Double2D;

Double2D way0(r, Double1D(c));
Double2D way1(r, Double1D(c));
Double2D way2(r, Double1D(c));
Double2D way3(r, Double1D(c));
Double2D lev2(r1, Double1D(c));

现在,这会创建远离堆栈的数据,而不是堆上的数据。其余代码应该能够编译而无需进一步更改。

请注意,我没有检查您在程序的其余部分中使用的任何数组索引,因此您可能仍会在其余代码中的某处访问数组访问。其他人评论说你使用rand()确实存在一些索引边界问题,但至少你不会遇到堆栈空间问题。