C++ array in Objective C gives EXC_BAD_ACCESS
arrays, c++, objective-c, xcode
Solution
I'd say its a very safe guess to assume you accessed an element outside of the array, i.e. your `i` was `>= capacity`. And yes, you will have to eventually manually `delete[]` the array you allocated using `new`. Definitely from this snippet you posted it looks like it will leak, so maybe you want to provide the code more in context, like where variables are defined (locally or in a class or wherever) and how data is accessed, etc.
Problem
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 ```