将消息发送到 C 数组中的 NSMutableArray 时出现问题

发布于 2024-12-01 07:52:40 字数 945 浏览 2 评论 0原文

我目前正在尝试实现一个池系统,我拥有所有代码,我只是不明白为什么它的某些部分不起作用。

我有一个 NSMutable 数组的 c 数组,如下所示:

    NSMutableArray *poolArray[xSize][ySize];
    for (int n = 0; n < xSize; n++) 
    {
        for (int m = 0; m < ySize; m++)
        {
            poolArray[n][m] = [[NSMutableArray alloc] init];
        }
    }

在尝试访问它时,我获取池和对象所在的 x 和 y 坐标,并尝试像这样添加它:

[poolArray[x][y] addObject:object]; //This raises a EXC_BAD_ACCESS error

我完全愿意编辑我的编写方式这 - 我知道我可以声明一个 NSMutableArray 并使用 ((y * width) + x) 的索引,我可能必须像这样重写代码。但最好我不想这样做,因为我只想实际创建我正在使用的数组,所以像这样:

if (poolArray[x][y] == nil) poolArray[x][y] = [[NSMutableArray alloc] init];
[poolArray[x][y] addObject:object];

这样它就可以有“洞”,所以我不必在 poolArray[2] 上做任何事情[3] 例如,如果那里什么都没有。

我不知道是否可以用 Objective-C 类型重写它,但如果我这样做,我就被迫在每个空间继续创建 NSMutableArray,我不想这样做的原因是因为我想充分利用系统的每一点性能。

感谢您花时间阅读本文,如有任何回复,我们将不胜感激:)

I'm currently trying to implement a pooling system, I have all the code, I just dont understand why a certain part of it doesn't work.

I have a c-array of NSMutable array made like this:

    NSMutableArray *poolArray[xSize][ySize];
    for (int n = 0; n < xSize; n++) 
    {
        for (int m = 0; m < ySize; m++)
        {
            poolArray[n][m] = [[NSMutableArray alloc] init];
        }
    }

And whilst trying to access it I get the x and y coordinate of the pool and object is in and try to add it like this:

[poolArray[x][y] addObject:object]; //This raises a EXC_BAD_ACCESS error

I am totally open to editing how I write this - I am aware that I could declare a NSMutableArray and use indexes of ((y * width) + x) and I may have to rewite the code like that. But preferably I dont want to have to do that as I only want to actually create the arrays I'm using so something like this:

if (poolArray[x][y] == nil) poolArray[x][y] = [[NSMutableArray alloc] init];
[poolArray[x][y] addObject:object];

This is so that it can have 'holes' so I dont have to make anything at poolArray[2][3] for example if there is nothing there.

I don't know if there is anyway that I could rewrite that with objective-c types, but if I do I'm forced to keep creating a NSMutableArray at every space, the reason I dont want to do that is because I want to get every little bit of performance I can out of the system.

Thanks for taking the time to read this, and any response is appreciated :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

我家小可爱 2024-12-08 07:52:40

这对我有用:

#import <Foundation/Foundation.h>

#define xSize 10
#define ySize 10

int main (int argc, const char * argv[])
{

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSMutableArray *poolArray[xSize][ySize];
    for (int n = 0; n < xSize; n++) 
    {
        for (int m = 0; m < ySize; m++)
        {
            poolArray[n][m] = [[NSMutableArray alloc] init];
        }
    }

    [poolArray[2][3] addObject: @"Hello"];
    [poolArray[2][3] addObject: @"world!"];
    NSLog(@"poolArray[2][3] objects: %@ %@", 
              [poolArray[2][3] objectAtIndex: 0],
              [poolArray[2][3] objectAtIndex: 1]);

    [pool drain];
    return 0;
}

(是的,我知道,我应该释放所有 NSMutableArray 实例。为简洁起见,省略)

因此,您应该检查以下几件事:

  • object 是否是有效的对象,即它是否已初始化? NSMutableArray 会尝试保留该对象,如果它从未初始化过,那么将会严重失败,或者如果它已经被释放了,也会失败。
  • xy 有效吗?您可以轻松地越过界限而不会注意到它。

This works for me:

#import <Foundation/Foundation.h>

#define xSize 10
#define ySize 10

int main (int argc, const char * argv[])
{

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSMutableArray *poolArray[xSize][ySize];
    for (int n = 0; n < xSize; n++) 
    {
        for (int m = 0; m < ySize; m++)
        {
            poolArray[n][m] = [[NSMutableArray alloc] init];
        }
    }

    [poolArray[2][3] addObject: @"Hello"];
    [poolArray[2][3] addObject: @"world!"];
    NSLog(@"poolArray[2][3] objects: %@ %@", 
              [poolArray[2][3] objectAtIndex: 0],
              [poolArray[2][3] objectAtIndex: 1]);

    [pool drain];
    return 0;
}

(Yes, I know, I should release all NSMutableArray instances. Left out for brevity).

So there are a few things you should check:

  • Is object a valid object, i.e. was it initialized? The NSMutableArray will try to retain the object, and if it was never initialized, that will fail miserably, or if it was dealloc-ed already, it will fail too.
  • are x and y valid? You can easily go over the boundaries and not notice it.
友欢 2024-12-08 07:52:40

尽管有一些想法,但看不出您提供的代码有任何问题:

在检查 poolArray[x][y] == nil 的情况下,您在初始化数组时实际上将所有值重置为 nil ?

另一种可行的方法是将数组存储在堆上。您可以使用 calloc (它将内存初始化为 0),或 malloc 和 memset。

以下应该有效:

NSMutableArray ***poolArray = calloc(xSize * ySize, sizeof(NSMutableArray *));

if (poolArray[x][y] == nil) poolArray[x][y] = [[NSMutableArray alloc] init];
[poolArray[x][y] addObject:object];

Can't see anything wrong with the code you've provided, although a couple of ideas:

In the case where your checking poolArray[x][y] == nil have you actually reset all the values to nil when you initialize the array?

An alternative that should work, is to store the array on the heap. You could use calloc (which will initialize the memory to 0), or malloc and memset.

The following should work:

NSMutableArray ***poolArray = calloc(xSize * ySize, sizeof(NSMutableArray *));

if (poolArray[x][y] == nil) poolArray[x][y] = [[NSMutableArray alloc] init];
[poolArray[x][y] addObject:object];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文