Re: Problem instantiating an array
Re: Problem instantiating an array
- Subject: Re: Problem instantiating an array
- From: Dennis <email@hidden>
- Date: Tue, 7 Dec 2010 10:11:24 -0800
Hi Ken,
Thanks for taking a look at my code.
On Dec 6, 2010, at 10:35 PM, Ken Thomases wrote:
> If you step after the line "TileColumn *allColumns[boardDimension - 1]", nothing has happened, yet. That's just a declaration.
But if for example I declare int test[16], the debugger shows the array "test" with 16 members. That's not happening in the case of my code with allColumns. It's displaying an array of -1 members.
> Or am I misunderstanding, and you mean you have stepped repeatedly after that line, through the loop?
Yes, exactly.
> Also, what you mean "array remains with a member count of -1"? Which array? How are you assessing the member count?
I'm talking about allColumns. When I inspect allColumns in the debugger after stepping over the line "TileColumn *allColumns[boardDimension - 1]", the debugger shows -1 for the array member count. That's how I'm assessing it.
> The allColumns array is a C array, which doesn't know its element count.
That doesn't make any sense to me. As mentioned above, if I declare an int array with a member count, it displays as such in the debugger.
> 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).
I understand that the NSArray gameColumns is nil until I assign to it. Unfortunately I am assigning a C array with -1 elements to it because the declaration TileColumn *allColumns[boardDimension - 1] allocates an array with -1 members as displayed in the debugger.
>> 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?
By looking at allColumns in the debugger. When I look at other code in the debugger that works properly to create and populate an array, I can see objects being added to the array. So why do you say it's impossible to determine 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.
No. It's a debug build.
> 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.
I experimented with doing that unsuccessfully.
I am beginning to think that the problem is with having declared boardDimension at the top of my file. I did another experiment and added: int test[boardDimension]; to the file. After stepping over that in the debugger, it showed an array with -1 members. I changed it to int test[16], stepped over that and the debugger showed an array with 16 members. Am I misunderstanding how the debugger works? If not, why is it now working to declare boardDimension at the top of the file?_______________________________________________
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