Objective c 在iOS应用程序中写入C代码时EXC访问错误

Objective c 在iOS应用程序中写入C代码时EXC访问错误,objective-c,c,Objective C,C,我试图在ios应用程序中使用纯c编程语言实现一些功能。当方阵大小为50(w=h=50)时,代码运行良好。如果我将矩阵的大小增加到100,则会收到EXC错误访问消息。下面是我正在使用的代码: double solutionMatrixRed[w][h]; double solutionMatrixGreen[w][h]; double solutionMatrixBlue[w][h]; double solutionMatrixAlpha[w][h]; f

我试图在ios应用程序中使用纯c编程语言实现一些功能。当方阵大小为50(w=h=50)时,代码运行良好。如果我将矩阵的大小增加到100,则会收到EXC错误访问消息。下面是我正在使用的代码:

    double solutionMatrixRed[w][h];
    double solutionMatrixGreen[w][h];
    double solutionMatrixBlue[w][h];
    double solutionMatrixAlpha[w][h];

    for(int x=0;x<w;x++)
    {
        for(int y=0;y<h;y++)
        {

            //NSLog(@"x=%d y=%d",x,y);
            solutionMatrixRed[x][y] = 0;
            solutionMatrixGreen[x][y] = 0;
            solutionMatrixBlue[x][y] = 0;
            solutionMatrixAlpha[x][y] = 0;
        }
    }
double solutionMatrixRed[w][h];
双溶液基质绿[w][h];
双溶液基质蓝[w][h];
双溶液基质α[w][h];

对于(int x=0;x您的代码在自动存储器*中分配所有四个矩阵,这可能是有限的。对于移动设备来说,即使是四次80K也可能太多

如果需要处理那么多内存,请考虑使用<代码> MaloC/ >从动态内存分配它:

double (*solutionMatrixRed)[h] = malloc((sizeof *solutionMatrixRed) * w);
// allocate other matrices in the same way, then do your processing
free(solutionMatrixRed); // Do not forget to free the memory.


*通常称为“堆栈”,以自动存储实现中经常使用的数据结构的名称命名。

我认为iOS中的堆栈大小限制为512 KB。在w=100和h=100时,您的数组将需要大约312.5 KB。我怀疑您超出了堆栈大小,应该尝试在堆上分配数组(使用malloc()分配数组)。

因为您试图分配堆栈上的所有内存。 虽然您应该使用动态分配(malloc)在堆上分配它:

double**solutionMatrixRed=malloc(h*sizeof(double*);

对于(i=0;问题不在这部分代码中。正如您看到的,x总是小于w,y小于h。您是否尝试在堆上而不是堆栈上分配数组?如果我想将solutionMatrixRed中的每个条目初始化为零,我可以简单地编写solutionMatrixRed[x][y]=0并使用两个for循环进行迭代,外部for循环用于x,内部for循环用于y。@user1464587绝对-请看一个示例。
double **solutionMatrixRed = malloc(h * sizeof(double *));
for(i=0; i<h; i++)
    solutionMatrixRed[i] = malloc(w * sizeof(double));