2D 动态内存分配 - ObjectiveC

发布于 2024-10-06 04:56:55 字数 102 浏览 1 评论 0原文

我需要一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

逆流 2024-10-13 04:56:55

将 C 数组和 NSArray 串在一起很奇怪,但这是可以做到的:

NSMutableArray *myArrays[];
myArrays = malloc(sizeof(NSMutableArray *) * numberOfArrays);

更好的解决方案是使用 NSArray 的 NSArray;或者使用 NSArray 的 NSArray。

NSMutableArray *rows = [NSMutableArray array];
[rows addObject: [NSMutableArray array]];
[rows addObject: [NSMutableArray array]];
[rows addObject: [NSMutableArray array]];

NSMutableArray *row0 = [rows objectAtIndex: 0];
[row0 addObject: [Datum new]];
[row0 addObject: [Datum new]];
[row0 addObject: [Datum new]];

.... 等等 ....

或者,直接使用指针数组:

Datum **my2DArray = malloc(sizeof(Datum *) * width * height);

然后,my2DArray 中任何给定的笛卡尔坐标都是一个简单的数学运算:

my2DArray[ x + (y * width) ] = ....;

这将有效地转换任何给定的 (x ,y) 坐标转换为线性索引(实际上,y 成为步幅,x 成为步幅内的偏移量)。

It is weird to collude C arrays and NSArrays, but it can be done:

NSMutableArray *myArrays[];
myArrays = malloc(sizeof(NSMutableArray *) * numberOfArrays);

A better solution would either be to use an NSArray of NSArrays;

NSMutableArray *rows = [NSMutableArray array];
[rows addObject: [NSMutableArray array]];
[rows addObject: [NSMutableArray array]];
[rows addObject: [NSMutableArray array]];

NSMutableArray *row0 = [rows objectAtIndex: 0];
[row0 addObject: [Datum new]];
[row0 addObject: [Datum new]];
[row0 addObject: [Datum new]];

.... etc ....

Or, just use an array of pointers directly:

Datum **my2DArray = malloc(sizeof(Datum *) * width * height);

Then, any given cartesian coordinate within my2DArray is a simple bit of math:

my2DArray[ x + (y * width) ] = ....;

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).

楠木可依 2024-10-13 04:56:55

嗯,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.

最美不过初阳 2024-10-13 04:56:55

对于 NSArrays,您可以使用这样的内容:

NSMutableArray *array2d = [[NSMutableArray alloc] init];
for (int i = 0; i < d1; i++)
{
      NSMutableArray *innerArray = [NSMutableArray array];
      [array2D addObject:innerArray];

      for (int j = 0; j < d2; j++)
      {
           [innerArray addObject:myDynamicData];
      }
}

如果您通过动态数据表示其他内容,请编辑 OP 准确解释您的意思。

For NSArrays, You can use something like this:

NSMutableArray *array2d = [[NSMutableArray alloc] init];
for (int i = 0; i < d1; i++)
{
      NSMutableArray *innerArray = [NSMutableArray array];
      [array2D addObject:innerArray];

      for (int j = 0; j < d2; j++)
      {
           [innerArray addObject:myDynamicData];
      }
}

If you mean something else by dynamic data, then please edit the OP explaining exactly what you mean.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文