views:

240

answers:

2

I'm trying to improve performance for my application, and I'd like to change from the NSMutableArray I'm currently using to a dynamically allocated C array.

As a test, I've created this class:

In my class interface, I have:

MyObject *myObjectArray;

In my implementation, after the object has been initialised, I have another method to set up the array:

-(void) createObjectsWithNumberOfObjects:(int)numberOfObjects
{
    MyObject *tempObject = nil;
    myObjectArray = (MyObject*) malloc(numberOfObjects * sizeof(tempObject));
    for( int i = 0; i < numberOfObjects; i++ )
        tempObject = [[MyObject alloc] init];
        myObjectArray[i] = (MyObject*) tempObject; // error
    }
}

I'm getting an error of: Incompatible Types In Assignment

So what am I doing wrong?

Is using a dynamically allocated array of objects like this possible?

Also, In my dealloc method, I have it setup to call release on each object then free(myObjectArray)

+2  A: 

You need some more *s. Declare your array pointer as:

MyObject **myObjectArray;

And then initialize it like this:

myObjectArray = (MyObject**) malloc(numberOfObjects * sizeof(MyObject *));

Leave everything else in your code the same as it is now and you should be good! Why do you want to change from NSMutableArray? It's a lot more capable in terms of flexibility and Cocoa support than your C array will be.

Carl Norum
Sweet! It works!I'm trying to squeeze as much performance out of the array as I can for a game.I still use NSArrays/NSMutable arrays for other things in the app - I do like thier ease of use and functionality.Thanks!
codedeziner
@Big Dogg, If you have performance problems, I can all but guarantee that `NSMutableArray` is not the reason...
Carl Norum
No performance problems (yet). I'm curious about the performance gain of using a standard C array vs. NSMutableArray.With around a hundred objects being accessed every frame, I would think there would be some kind of performance improvement.However, you are correct in that if a lag occurs, it will likely be somewhere else.
codedeziner
If you really are concerned about performance, I'd suggest avoiding premature optimization and instead profiling your code with Instruments and Shark. You might be surprised at what actually is the bottleneck in your application.
Brad Larson
A: 

Are you really sure you will improve performance much? NSMutableArray has been around a long time and is likely to be more intelligent than you at this point in terms of allocating more memory for new elements.

Now if you were creating a linked list or something, that could yield a benefit for things like inserts in the middle... but for just a plain array you can add on to, I'm not sure how much gain you will see.

Kendall Helmstetter Gelner
It's not about the allocation of memory, but more about the processing time of iterating through each element.When having to access many objects 60 times a second, I'd think a standard C array would be faster than using NSMutableArray.however, I could be wrong...
codedeziner