c - 为什么这个数组不够大?

标签 c memory

我已经运行这个随机游走模拟一段时间了,但我一直从 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/

相关文章:

c - HDF5 可变长度结构,可变长度(C API)

c - 从 SCANF 返回 EOF

c - strpbrk 的行为不符合预期

java - 解析/扩展数千个 URL 的工具/库

Linux 使用交换而不是 RAM 进行大图像处理

linux - 为什么RAM的写入速度远低于上面所说的?

c++ - Arduino双命令

c - 将字符串数组传入函数+此数组的动态分配不起作用

Java 内存错误 : unable to create new native thread

c++ 创建新对象时 : iterator not incrementable