我已经运行这个随机游走模拟一段时间了,但我一直从 Xcode 收到错误 EXC_BAD_ACCESS
。不过,它会打印出大部分模拟。
我认为它出于某种原因耗尽了内存,但我不确定为什么。
如果我走到数组的末尾,我会编辑它,这样我就不会到达边缘的 100 个空格内(通过将可变步骤编辑为步骤 -100)。这有效,但我想知道发生了什么。
如有任何帮助,我们将不胜感激。
double** places;
places = (double**) malloc(steps*sizeof(double*));
for (int i = 0; i < steps; i++)places[i] = (double*) malloc(2*sizeof(double));
for (i = 0; i< steps/*, exit*/; i++) {
// Find the angle of movement
angle = getRand()*360;
// Take a step
xPos+= STEP*cos(angle);
yPos+= STEP*sin(angle);
//Write Step to array
places[i][1] = xPos;
places[i][2] = yPos;
//Write Step to File
fprintf(ff, "%d %lf %lf\n",i,xPos,yPos);
}
最佳答案
数组索引从零开始。
你是想写这个吗?
places[i][0] = xPos; //Zeroth element is considered the first element
places[i][1] = yPos; //Second element
关于c - 为什么这个数组不够大?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8775721/