• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Problem instantiating an array
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

  • Follow-Ups:
    • Re: Problem instantiating an array
      • From: Dennis <email@hidden>
    • Re: Problem instantiating an array
      • From: Graham Cox <email@hidden>
References: 
 >Problem instantiating an array (From: Dennis Birch <email@hidden>)

  • Prev by Date: Re: OS X Desktop
  • Next by Date: Re: Problem instantiating an array
  • Previous by thread: Problem instantiating an array
  • Next by thread: Re: Problem instantiating an array
  • Index(es):
    • Date
    • Thread