2D 动态内存分配 - ObjectiveC
我需要一些 iPhone sdk 中 ObjectiveC 中二维数组动态内存分配的示例。示例代码将不胜感激。我想声明一个指针数组,然后每个索引将在运行时声明一个数组。
谢谢
I need some examples of dynamic memory allocation of 2D array in ObjectiveC in iPhone sdk. Sample code will be appreciated. I want to declare an array of pointers and then each index will be declare an array at runtime.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将 C 数组和 NSArray 串在一起很奇怪,但这是可以做到的:
更好的解决方案是使用 NSArray 的 NSArray;或者使用 NSArray 的 NSArray。
.... 等等 ....
或者,直接使用指针数组:
然后,
my2DArray
中任何给定的笛卡尔坐标都是一个简单的数学运算:这将有效地转换任何给定的 (x ,y) 坐标转换为线性索引(实际上,y 成为步幅,x 成为步幅内的偏移量)。
It is weird to collude C arrays and NSArrays, but it can be done:
A better solution would either be to use an NSArray of NSArrays;
.... etc ....
Or, just use an array of pointers directly:
Then, any given cartesian coordinate within
my2DArray
is a simple bit of math:That'll effectively convert any given (x,y) coordinate into a linear index (effectively, y becomes the stride and x becomes the offset within the stride).
嗯,C 当然有动态内存分配。看看 malloc 和 realloc。有了这个,您可以分配内存并在需要时调整大小,您只需跟踪所有内存使用情况,并且完成后不要忘记 free() 。
Umm of course C has dynamic memory allocation. Look at malloc and realloc. With this you can allocate memory and resize later if needed, you just have to keep track of all your memory usage and don't forget to free() when you are done.
对于 NSArrays,您可以使用这样的内容:
如果您通过动态数据表示其他内容,请编辑 OP 准确解释您的意思。
For NSArrays, You can use something like this:
If you mean something else by dynamic data, then please edit the OP explaining exactly what you mean.