Sorry if this is a bit of a C-noob question: I know that I need to stroke my pointers. Unfortunately, I am in a deadline, so I donβt have time to work on a complete book chapter, so I hope for more focused advice.
I want to store some objective-C objects in an array C. I use ARC. If I were on a Mac, I could use NSPointerArray, but I'm on iOS and not available.
I will store a three-dimensional array of C: conceptually my measurements are day, height and cacheNumber. Each element will be either a pointer to an objective-C object, or NULL.
The number of caches (i.e., the size dimension of the cache size) is known at compile time, but the other two are unknown. Also, the array can be very large, so I need to dynamically allocate memory for it.
Regarding ownership semantics, I need strong object references.
I would like the entire three-dimensional array to be an instance variable of an objective-C object.
I plan to have a method - tableForCacheNumber:(int)num days:(int*)days height:(int*)height . This method should return a two-dimensional array, i.e. one specific cache number. (It also returns by reference the size of the array that it returns.)
My questions:
In what order should my measurements be placed so that I can easily return a pointer to a subarray for one specific cache number? (I think this should be the first, but I'm not 100%.)
What should be the return type of my method so that ARC does not complain? I don't mind if the returned array has an increased reference count or not, if I know what it does.
What type should an instance variable that contains a three-dimensional array have? I think this should be just a pointer, since this ivar just represents a pointer to the first element that is in my array. Right? If so, how can I indicate this?
When I create a three-dimensional array (for my ivar), I think I'm doing something like calloc(X * Y * Z, sizeof(id)) , and pass the result to the type for my ivar?
When accessing elements from a three-dimensional array in ivar, I believe that every time you need to play a pointer with something like (*myArray)[4][7][2] . Correctly?
Will there be similar access to the two-dimensional array returned by my method?
Do I need to mark the returned two-dimensional array objc_returns_inner_pointer ?
I regret again that this is a bit of a bad question (it is too long and with too many parts). I hope EU citizens will forgive me. To improve my interweb karma, I may write it as a blog post when this project is submitted.
ios objective-c automatic-ref-counting
Amy worrall
source share