在Objective C(iOS版)VLA变长数组,会发生什么? [英] VLA Variable-length array in Objective C (iOS), what happens?

查看:214
本文介绍了在Objective C(iOS版)VLA变长数组,会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一款iOS应用,维护一些code,我来到了以下内容:

Maintaining some code from an iOS application, I came upon the following:

CLLocationCoordinate2D inputArray[size]; // CLLocationCoordinate2D is a struct containing two doubles
for (int i = 0; i < size; i++) {
    inputArray[i] = ... ; // Fill the array
}
CLLocationCoordinate2D outputArray[size];
functionThatConvertsInputToOutput(inputArray, outputArray, size);

下面我们正在做动态大小的两个结构数组分配(不能确定在编译时的大小)。所谓的变长数组的基础上有这么一个问题(声明动态数组)。

Here we are doing allocation of two struct arrays of dynamic size (cannot determine size at compile time). So called "Variable-length array", based on that SO question ( Declare Dynamic Array ).

我很清楚,这不,即使在C / C ++编译和类似的问题后看时,得到的回答往往是用malloc或使用NS(可变)阵列。

I'm well aware that this does not even compile in C/C++ and when looking after similar questions, the answer is often "Use malloc" or "Use NS(Mutable)Array".

但我还没有真正找到问题的答案:

But I haven't really found the answer to the question:

宣布 int数组【尺寸】当目标C会发生什么;

What happens in Objective C when declaring int array[size]; ?

我不知道的原因是,这块code。使用VLA时合理的大尺寸(36000),并使用malloc时不会崩溃我上面崩溃转载:

The reason I'm wondering is that the piece of code I have reproduced above crashes when using VLA with reasonably large sizes (36000) and does not crash when using malloc:

CLLocationCoordinate2D *inputArray = malloc(sizeof(CLLocationCoordinate2D) * size);
CLLocationCoordinate2D *ouputArray = malloc(sizeof(CLLocationCoordinate2D) * size);

编辑#1:维基百科说,大约VLA http://en.wikipedia.org/wiki什么/可变length_array

编辑#2:崩溃是 EXC_BAC_ACCESS functionThatConvertsInputToOutput 奇怪的地方,或在排队叫号 functionThatConvertsInputToOutput

EDIT #2: Crashes are EXC_BAC_ACCESS at odd places in functionThatConvertsInputToOutput or on the line calling functionThatConvertsInputToOutput.

推荐答案

这是非常有可能的坚持的内存堆栈上的数组,这就是为什么你当您通过3600​​0炸毁堆栈崩溃* sizeof的(CLLocationCoordinate2D )。

It’s very likely it’s sticking the memory for the array on the stack, which is why you’re crashing when you blow up the stack by 36,000 * sizeof(CLLocationCoordinate2D).

这篇关于在Objective C(iOS版)VLA变长数组,会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆