0

I'm using this code to create some objects and then store them in an array

for (int iy=0; iy<5; iy++) {
        for (int ix=0; ix<5; ix++) {

            TerrainHex *myObject = [[TerrainHex alloc] initWithName:(@"grassHex instance 10000") width:mGameWidth height:mGameHeight indexX:ix indexY:iy];
            myObject.myImage.y += 100;

            [TerrainHexArray addObject:myObject];

            [self addChild:(id)myObject.myImage];
        }
    }
    NSLog(@"%lu", sizeof(TerrainHexArray));

Few questions.

  1. The log is only displaying 4, which makes no sense, shouldn't it be 5x5, i'e 25?
  2. Am I creating 25 seperate object pointers there or just re-using the same one over and over? I'm trying to save all 25 pointers into an array.
  3. I'm using ARC but do I have to release anything there?
Phil
  • 2,995
  • 6
  • 40
  • 67
  • 1
    It's my strong belief that 5 x 5 is 25, not 20. – jscs Jul 09 '12 at 19:21
  • I just changed it to NSLog(@"Terrain array: %u", [TerrainHexArray count]); and the result it's giving is zero. – Phil Jul 09 '12 at 20:55
  • You need to create the array. http://stackoverflow.com/questions/7125326/cannot-add-items-to-an-nsmutablearray-ivar http://stackoverflow.com/questions/3683761/nsmutablearray-addobject-not-affecting-count http://stackoverflow.com/questions/1827058/nsmutablearray-addobject-not-working – jscs Jul 09 '12 at 20:59

2 Answers2

2

The log is only displaying 4, which makes no sense, shouldn't it be 5x5, i'e 20?

use [TerrainHexArray count] to get the amount of objects in the array. sizeof(TerrainHexArray) gives you the size of an id *, which is 4 bytes in your system.

Am I creating 20 seperate object pointers there or just re-using the same one over and over? I'm trying to save all 20 pointers into an array.

You are creating 25 objects

I'm using ARC but do I have to release anything there?

No.

MByD
  • 135,866
  • 28
  • 264
  • 277
2
  1. sizeof() tells you the size in bytes of the variable TerrainHexArray which is (presumably) a pointer to an NSMutableArray. Assuming a 32-bit system, pointers are 32 bits which is 4 bytes. You should be using [TerrainHexArray count] instead. That's a method that returns the number of objects in the array.

  2. You're creating 25 object instances, not the same one over and over. myObject is just a variable holding a pointer to a given object. Changing it by assignment doesn't obliterate the object it pointed to before (though ARC takes care of releasing it).

  3. No, ARC takes care of memory management for you.

One nitpick: Assuming TerrainHexArray is an instance of NSArray, you shouldn't capitalize the first letter. This isn't a requirement of the language, but it is convention to capitalize class names, but use a lower case first letter for variable names. terrainHexArray would be more appropriate and make the code more readable.

Andrew Madsen
  • 21,309
  • 5
  • 56
  • 97
  • I usually would do terrainHexArray in AS3, but in obj-c thought maybe you capitalize the first letter, is there a list of conventions anywhere for obj-c? – Phil Jul 09 '12 at 19:11
  • Apple has some documentation: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CodingGuidelines/Articles/NamingIvarsAndTypes.html#//apple_ref/doc/uid/20001284-1001757 This is a good (two part) article: http://cocoadevcentral.com/articles/000082.php . Do note that the second article isn't definitive, and I noticed one thing that's now out-of-date (ivar names prefixed with an underscore are fine -- even encouraged -- lately). – Andrew Madsen Jul 09 '12 at 20:58