我一直在运行这个随机游走模拟一段时间,我一直从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);
}
哦,谢天谢地!现在我觉得自己像个白痴。我知道这一点,但我完全混淆了它。 – Treesrule14 2012-01-08 06:04:43
没问题。它发生在我们最好的:) – tangrs 2012-01-08 06:06:10