2

I'm still very much a noob, having a lot of fun learning the basics of Objective-C, using XCode to put together some simple programs for OS-X.

I have a program which ranks a five card poker hand.

Each card in the deck is identified by its unique 'index number' (0-51)

To speed up the evaluator I thought it would be useful to have an array containing all possible combinations of five indices (there are 2598960 of these).

If I do this:

    NSMutableArray *allPossibleHands = [[NSMutableArray alloc] initWithObjects: nil];

    for(int i = 0; i<48; i++)
    {
        for(int j = i+1; j<49; j++)
        {
            for(int k = j+1; k<50; k++)
            {
                for(int m = k+1; m<51; m++)
                {
                    for(int n = m+1; n<52; n++)
                    {
                        NSNumber *number0 = [NSNumber numberWithInt: i];
                        NSNumber *number1 = [NSNumber numberWithInt: j];
                        NSNumber *number2 = [NSNumber numberWithInt: k];
                        NSNumber *number3 = [NSNumber numberWithInt: m];
                        NSNumber *number4 = [NSNumber numberWithInt: n];

                        NSArray *nextCombination = [[NSArray alloc] initWithObjects: number0,number1,number2,number3,number4,nil];
                        [allPossibleHands addObject: nextCombination];
                    }
                }
            }
        }
    }

    NSLog(@"finished building allPossibleHands. It contains %i objects", [allPossibleHands count]

);

everything seems to work fine, and I get a message to say that my array contains, as expected, 2598960 objects. I can then list all the elements of my array.

But I thought wrapping my ints in NSNumber objects like that must take up a lot more memory. Maybe storing the index numbers as short ints would be better.

However, if, instead of building my array as above, I do this:

`short int allPossibleHands[2598960][5]`;

intending to then use my loop to store the ints directly, I get a EXC_BAD_ACCESS error message and a note that there's no memory available to the program.

So how come I can store all those NSNumber objects, but not the ints?

Is there some rule about array construction that I'm breaking?

As always, any guidance much appreciated.

Thank You for reading this.

3 Answers3

4

While the second is allocated on the stack (which is much more limited in size), the first one is allocated on the heap and is a pointer to a memory area.

This does not mean that the first one takes less space. If you allocated the second array as a pointer, the error would go away.

Also read the answers to this question.

Community
  • 1
  • 1
Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
  • @Sam Pollard: Don't forget to mark the answer as correct, in all your questions you should have the best answer marked. http://meta.stackexchange.com/q/5234/160504 – sidyll Jul 28 '11 at 13:45
1

I think the problem is where you are storing your array. Is it on the stack? If so, keep in mind it's going to be 25MB so much larger than most stacks allow.

dascandy
  • 7,184
  • 1
  • 29
  • 50
1

Assuming that an NSNumber object must store the value and the type of the number, it is probably a little larger than an int.

But if your int[][] array is a local variable, it is very likely stored on the stack, and most stacks are not that large. You could use a pointer to such an array and malloc it on the heap, which probably has enough room for it.

Accessing a C array is very likely a little faster than accessing that many NSNumbers in an NSArray and extracting their values, and if this is for a card game, speed is probably an issue.

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
  • Thanks for your help. Much appreciated. I'm a total noob when we start talking about heaps and stacks, but I feel sure that a C array would be a quicker way of doing things. (I'm working my way towards an equity calculator, which will involve dealing a lot of hands, the quicker the better.) Your response has given me useful stuff to look at next. Thanks again. – Sam Pollard Jul 28 '11 at 11:21
  • malloc-ing it on the heap looks like the way to go. With a 2-d array of NSNumber objects I was evaluating just under 1 million hands per second. (I should add that someone else wrote the evaluation code. I'm just working on the best way to feed it hands.) After sticking everything into a 1-d int array, I'm getting roughly 4 million per second. Very nice. Thanks again, to you, and to everyone who responded, particularly as I can see that this is a somewhat common question. I've learned something very useful here. – Sam Pollard Jul 28 '11 at 13:26