gpt4 book ai didi

c++ - 我的 C++ 模拟器使终端崩溃

转载 作者:行者123 更新时间:2023-12-01 22:37:35 25 4
gpt4 key购买 nike

每次执行我的代码都会使终端崩溃。我已经尝试使用 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;

}

任何帮助将不胜感激,因为我不再有任何学术帮助

最佳答案

立即出现一个问题:您很可能会因为 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 并相应地调整大小。

#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() 确实存在一些索引边界问题,但至少不会遇到堆栈空间问题。

关于c++ - 我的 C++ 模拟器使终端崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23022511/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com