Re: Problem instantiating an array
Re: Problem instantiating an array
- Subject: Re: Problem instantiating an array
- From: Ken Thomases <email@hidden>
- Date: Tue, 7 Dec 2010 00:35:21 -0600
Hi,
On Dec 5, 2010, at 12:39 PM, Dennis Birch wrote:
> - (id)initWithFrame:(NSRect)frame {
> self = [super initWithFrame:frame];
> if (self) {
> // Initialization code here.
> TileColumn *allColumns[boardDimension - 1];
The above declaration is wrong for what you're trying to accomplish. You want the array to have boardDimension elements, not boardDimension - 1 elements.
> for (int i = 0; i < boardDimension; i++)
> {
> TileColumn *column = [[TileColumn alloc] init];
> allColumns[i] = column;
On the last pass through this loop, when i == boardDimension - 1, you're writing past the end of the array. Since you've declared the array to have boardDimension - 1 elements, the largest valid index into it is boardDimension - 2.
> }
>
> gameColumns = [NSArray arrayWithObjects:allColumns count: boardDimension];
Here, NSArray will read past the end of allColumns. allColumns has been declared with boardDimension - 1 elements, but you're telling NSArray that it has boardDimension elements.
> }
> return self;
> }
> I've run into a showstopper with my first project on my own (i.e. not a project from a book). I've created a custom View subclass, and when I try to instantiate an array of another custom subclass, it completely fails to do so, but doesn't produce any meaningful errors. I can see that it's failing in the debugger because after stepping after the line "TileColumn *allColumns[boardDimension - 1]", the array remains with a member count of -1.
If you step after the line "TileColumn *allColumns[boardDimension - 1]", nothing has happened, yet. That's just a declaration.
Or am I misunderstanding, and you mean you have stepped repeatedly after that line, through the loop?
Also, what you mean "array remains with a member count of -1"? Which array? How are you assessing the member count? The allColumns array is a C array, which doesn't know its element count. The gameColumns array, which I assume is an instance variable of type NSArray*, is nil until you assign to it in the last statement before the return. After that assignment, it will be a pointer to an NSArray with boardDimension elements (or it will have failed with an exception if something is very wrong, like some element of allColumns is still nil).
> In the for loop, the line "allColumns[i] = column;" doesn't produce any errors but doesn't change the array.
Doesn't change which array? allColumns? That's basically impossible. How are you determining this?
Is there any chance that you're trying to debug a release build (or any build that has optimizations enabled)? An optimized build will be very confusing to debug.
Finally, something to consider: there's nothing wrong with the technique you've used of populating a C array and then building an NSArray from that C array. However, you might consider creating an NSMutableArray that starts empty and then, during the for loop, each TileColumn is added to it. If you create the NSMutableArray with +arrayWithCapacity: or -initWithCapacity:, you'll even avoid reallocation as you add elements to the array.
Cheers,
Ken
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden