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 16:00:10 -0600
On Dec 7, 2010, at 12:11 PM, Dennis wrote:
> Thanks for taking a look at my code.
Sure.
> 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.
As others have pointed out, that doesn't say anything about the code, it says something about the debugger.
>> 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 debugger may know the element count of a fixed-size C array, but the array itself is a "dumb" data structure. You can't query a C array for its element count in code. Also, the number of elements in the array is (potentially) a different question than the capacity of the array. You seemed to be saying that you were expecting the array to know or express how many elements you had assigned into it. It doesn't know that.
That is, even if the debugger knew the size of allColumns, that wouldn't change as you assigned things into it. It would be a fixed size and only the contents of each position in it might change. So, stepping through your loop expecting the debugger to say "now there's 1 element in the array, now there are 2 elements, etc.", which is what it seemed your were saying, is unreasonable.
>> 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.
The expression you used to assign to gameColumns read boardDimension elements from the C array. So, it's simply not true that you were "array with -1 elements to it". There's no such thing as a C array with negative elements.
>>> 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.
Well, you see object pointers being assigned into the positions within the array. You can't "add" to a C array.
> So why do you say it's impossible to determine this?
I was saying that it's impossible for the statement "allColumns[i] = column;" to not affect the array, assuming 'i' is within the array's bounds.
Regards,
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