Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 创建字符数组后访问错误_Objective C_Arrays_Memory Management - Fatal编程技术网

Objective c 创建字符数组后访问错误

Objective c 创建字符数组后访问错误,objective-c,arrays,memory-management,Objective C,Arrays,Memory Management,我的申请似乎达到了极限,但我感到困惑 向下编辑,问题代码似乎是: NSInteger tcMax = 9000000; // 8 million here and all is ok. 9 or more = crash char tcBuffer[tcMax]; [self doSomething]; // EXC BAD ACCESS here. Or whatever other line of code is here 我已经将这些行粘贴到一个新的项目中,一切都很好,所以似乎还有

我的申请似乎达到了极限,但我感到困惑

向下编辑,问题代码似乎是:

NSInteger tcMax = 9000000;  // 8 million here and all is ok. 9 or more = crash
char tcBuffer[tcMax];

[self doSomething];  // EXC BAD ACCESS here. Or whatever other line of code is here

我已经将这些行粘贴到一个新的项目中,一切都很好,所以似乎还有其他因素在起作用。特定方法可以分配的总字节是否有一个最大值?或者在这个8/9 Mb的点附近我可能会达到的其他限制?

我前面没有Mac,但我相当确定9 Mb的堆栈分配太大了。在一个固定大小的数组中分配这么多内存会导致堆栈崩溃(因此,堆栈溢出)。转换为堆分配:


在这方面,iOS可能与MacOS有不同的行为

动态程序内存有限(最大值不同)


对于大型数组,使用
malloc
free
来使用堆。

这样分配的数组最终会出现在堆栈上,在堆栈上放置8-9 Mb绝对不是一个好主意。使用
malloc
char* tcBuffer = (char*)malloc(tcMax);

[self doSomething];

// before the function/method returns call this:
free(tcBuffer);
tcBuffer = NULL;