堆中的Objective C中的2D基本数组 [英] 2D Primitive Arrays in Objective C on the heap

查看:63
本文介绍了堆中的Objective C中的2D基本数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要堆上的原始2D浮点数组.我可以使用NSMutableArray作为包装器,但是为了提高性能,我只想使用基元.

I want a primitive 2D float array on the heap. I could use NSMutableArray as a wrapper, but for performance, I want to work with just primitives.

在Objective-C中使用对象的一种方法:

A way to do it in Objective-C WITH objects:

NSMutableArray *array = [NSMutableArray arrayWithCapacity:DIM1];

for (int i = 0; i < DIM1; i++) {
    array[i] = [NSMutableArray arrayWithCapacity:DIM2];
}

这就是我在C语言中要做的事情

This is what I would do in C:

float **array = new float*[DIM1];
for(int i = 0; i < DIM; i++) {
    hotplate[i] = new float[DIM2];
}

我将如何实现Objective-C?

How would I accomplish this is Objective-C?

推荐答案

Objective-C只是C之上的一组扩展.C部分(例如浮点数数组)未更改.您的问题是您在这里所说的"C"实际上是C ++.这是您在C语言中执行的操作:

Objective-C is just a set of extensions on top of C. The C parts (like arrays of floats) are unchanged. Your problem is that what you're calling "C" here is actually C++. This is how you do it in C:

float **array = malloc(DIM1 * sizeof(float*));
for(int i = 0; i < DIM1; i++) {
    hotplate[i] = malloc(DIM2 * sizeof(float));
}

(当然,实际的float数组此时会包含堆垃圾,因此您可能也希望对其进行初始化.)

(Of course, the actual float arrays contain heap garbage at this point, so you might want to initialize those too.)

这篇关于堆中的Objective C中的2D基本数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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