C++ array in Objective C gives EXC_BAD_ACCESS
I am using Xcode 4.6 programming for iOS with ARC.
I am working with an array of simple integers and when using
NSMutableArray it all works fine with the code below.
NSMutableArray *_array; // variable
...
_array = [[NSMutableArray alloc] initWithCapacity:capacity]; // allocation
...
[_array addObject:[NSNumber numberWithInt:int_number]]; // inserting
...
return [[_array objectAtIndex:i] integerValue]; // retrieving
This however does not perform that well as I am doing a lot of lookups in
the array and all the unboxing and possible NSMutableArray itself is a bit
slow.
In other parts of my code I have replaced the NSMutableArray with a simple
c-array with great improvements, but in this part I am working with
variable length arrays and cannot use a simple c-array. So I am trying
with C++ arrays. In Xcode i rename the file from .m to .mm (to compile as
Objective-C++) and use the code below which compiles fine but when it runs
it causes EXC_BAD_ACCESS errors and I cannot find the reason. Do I need to
do some manual garbage collection here?
int *_array; // variable
...
_array = new int[capacity]; // allocation
...
_array[i] = int_number; // inserting
...
return _array[i]; // retrieving
No comments:
Post a Comment