Random pointer issues

Kaamoss
Unregistered
 
Post: #1
Hello everyone, my name is Matt. This is my first post here. Anyways, I'm new to developing for the OS X platform as I just purchased a 12" ibook about a month ago. I know C++ and Java quite well and am trying to get the hang of objective-c. Anyways I decided a networked java texas holdem game in objective C would be a good place to start as I already made a decent one in java about 6 months ago. I seem to be having an issue however where my array of Card * objects are not being stored correctly or it's only storing a reference point which keeps dynamically changing. I've run through the debugger and it loads the card objects correctly into the array but when I goto print it, the cards are FUBAR. And my shuffle algorithm keeps giving me a SIGSEGV error and exits with code 11. Anyways I'm figuring that I'm just not grasping some fundamental concept regarding classes and pointers and the way objective-c handles copying and memory allocation/ deallocation. I'd be more than willing to throw the xcode project onto my website if someone here wouldn't mind letting me know what's up. I apologize if this in the wrong forum but I figure that this is a work in progress that has simply stopped progressing. Thanks for your time.

Matthew FitzPatrick
Quote this message in a reply
Member
Posts: 116
Joined: 2005.02
Post: #2
Hello and welcome to iDev. I'm just learning Obj-C but I know C++ and if you post the part of the code where the problem is, I'm sure I could help. Grin I really don't know how well Java and Obj-C link but it's worth a try. I'd like it if you threw the source on your site also, it would help.

[OFF-TOPIC](What a coincidence, my name is Matt too...)[/OFF-TOPIC]

Last login: Sat Aug 6 09:15:05 on console
Welcome to Darwin!
Matt-Chelens-Computer:~ matthew$
Quote this message in a reply
Kaamoss
Unregistered
 
Post: #3
Awesome I would appreciate any help, here is the link. Code Listing
Quote this message in a reply
Moderator
Posts: 133
Joined: 2008.05
Post: #4
I don't know anything about Objective-C, but you're problem may be in that you declare the Deck as Deck * cards[52]. You're saying there that you're going to have multiple decks of cards, each with 52 cards.

Then when you shuffle, you're also declaring it as a pointer to an array.

Like I said, I know nothing of Objective-C, but in C++ this would be wrong. You'd only need to declare a deck to be Deck cards[52], and allocate space for each card and just put them in the deck, or you would declare it as Deck* cards, and allocate space to the deck for 52 cards, then allocate space for each card and insert it into the deck. Either way, you're saying deck[X] = cardX to put the card in.

In C++, that'd actually give you a compiler error I think, since deck should be referenced as deck[i][j], and you're just putting something into deck[i]... or maybe you're code where you say deck[i] = [myCard copy], it's trying to force a type card into an un-allocated deck pointer.
Quote this message in a reply
Member
Posts: 116
Joined: 2005.02
Post: #5
There's that and I don't see anything telling it to be a J, Q, K, etc...unless you have 52 different card animations showing them that seem to be undeclared to be used...I also know little or none of Obj-C but know C++ and offered to help...

Last login: Sat Aug 6 09:15:05 on console
Welcome to Darwin!
Matt-Chelens-Computer:~ matthew$
Quote this message in a reply
Moderator
Posts: 133
Joined: 2008.05
Post: #6
Blorx2 Wrote:There's that and I don't see anything telling it to be a J, Q, K, etc...unless you have 52 different card animations showing them that seem to be undeclared to be used...I also know little or none of Obj-C but know C++ and offered to help...


Code:
for(i = 0; i < 4; i++){
        for(j = 0; j < 13; j++){
            myCard = [[Card alloc] init];
            [myCard setSuite: i];
            [myCard setValue: j];
            deck[i] = [myCard copy];
            [myCard release];
        }
    }

They're just unenumerated integers, can you even do enums or defines in cocoa, by the way? I think I should probably give in and learn it... I still won't give into http://www.thefacebook.com though, silly bastards.
Quote this message in a reply
Kaamoss
Unregistered
 
Post: #7
I tried re declaring Card * deck[52] to deck card[52] which gives me a parsing error. I assume the method which you [LongJumper] is flawed as I should have to declair deck[52] as a Card * type. in regards to the jack king queen issue I was considering using an enumerated type but for now I'm just trying to get this working so I figured I could assume that a value of 11, 12, and 13 can represent jack queen and king. I appreciate the help though.
Quote this message in a reply
Moderator
Posts: 133
Joined: 2008.05
Post: #8
Whoops, my bad. You should be saying Card deck[52] or Card* deck. An array of 52 cards, yup, my mistake, I just went over it quickly.
Quote this message in a reply
Kaamoss
Unregistered
 
Post: #9
See, I origionally thought that too, because in Java and C++ you can treat an instance of a class as if it were a data type just like an int or bool. In objective c if I change the Card * deck[52] to Card deck[52] deck.m starts to break as Card and Card* are not the same thing. if I try to change the meathods to return (Card) as opposed to (Card *) I get an unrecognized data type error. It's quite puzzling to me frankly. I think I'm just too used to higher level coding.
Quote this message in a reply
Kaamoss
Unregistered
 
Post: #10
ok so I fixed it here's how.
Code:
int i, j, tempsuite, tempval;
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    Card *myCard;
    for(i = 0; i < 4; i++){
        for(j = 0; j < 13; j++){
            myCard = [[Card alloc] init];
            [myCard setSuite: i];
            [myCard setValue: j];
            deck[i] = [myCard copy];
            [myCard release];
        }
    }

should be

Code:
int i, j, tempsuite, tempval, tempcounter;
        tempcounter = 0;
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    Card *myCard;
    for(i = 0; i < 4; i++){
        for(j = 0; j < 13; j++){
            myCard = [[Card alloc] init];
            [myCard setSuite: i];
            [myCard setValue: j];
            deck[tempcounter] = [myCard copy];
            [myCard release];
                        tempcounter++;
        }
    }

however I still am getting the SIGSEGV error on the shuffle call so if you see anything wrong with that and could let me know I'd be grateful. Thanks.
Quote this message in a reply
Moderator
Posts: 133
Joined: 2008.05
Post: #11
Well, I can't think of many higher level langues than Objective-C... but that's besides the point. Anyway, Card and Card* are not the same thing, so that is why. I don't know how to get the address in Cocoa, but in C++ to turn Card card1 into Card* cardRef, you'd simply just assign the address of it: cardRef = &card1; That also means that you can't change card1 at anytime.

Now that I think about it, I think @ is the address operator in Objective-C, but I don't really know.

Anyway, might want to remove the pointer to the arrays out of the declaration for the halves of each deck.

edit: You could change card1 at anytime, but that change takes place to cardRef too, but I'm sure you knew that, just pointing it out in case someone else reads it and thinks the wrong thing.
Quote this message in a reply
Member
Posts: 116
Joined: 2002.04
Post: #12
Code:
int i, j, tempsuite, tempval, tempcounter;
        tempcounter = 0;
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        Card *myCard;
        for(i = 0; i < 4; i++){
                for(j = 0; j < 13; j++){
                        myCard = [[Card alloc] init];
                        [myCard setSuite: i];
                        [myCard setValue: j];
                        deck[tempcounter] = [myCard copy];
                        [myCard release];
                        tempcounter++;
                }
        }

Note that your autorelease pool is having no effect here.

You create it, but then you don't use it.

To use it, you'd send an autorelease release message to an object, rather than a release. The next time the pool was walked, the object would be released.

However, that's probably not what you want. In this case, you probably want to use alloc and init to create the object and then release when you're completely done with it. That way, it will continue to stick around.

As for your question above about using Card, instead of Card * - that's a difference between Obj-C and C++. You use pointers to objects in Obj-C and do not declare objects on the stack as you would in C++.

Quote:can you even do enums or defines in cocoa,

You mean Obj-C, not Cocoa. And yes, since Obj-C is a superset of C, you cand do defines and enums.

Quote:Now that I think about it, I think @ is the address operator in Objective-C, but I don't really know.

Nope. Obj-C is a superset of C. Therefore the address of operator is &, just as it is in C. The @ operator is a special Obj-C operator to turn a literal string into an NSString.

Wade
Quote this message in a reply
Member
Posts: 208
Joined: 2005.04
Post: #13
Just in case you didn't know, there are 2 types of arrays in objective-C (at least, when you're using the Foundation framework). The first is your regular C array. This is where you'd write things like...

Code:
myArray[0] = 5;

But there is also another type of array. One which is much more high level (and I can see you've included the header for it): NSArray, and its mutable (changeable) counterpart, NSMutableArray. You use this type of array for arrays of objects. Here's an example:

Code:
NSMutableArray *myNSMutableArray = [[NSMutableArray alloc] init];

[myNSMutableArray addObject:@"Hello World"];

// use this instead of printf
NSLog(@"the object at index 0 is: %@", [myNSMutableArray objectAtIndex:0]);

// use 'release'.. not 'free'
[myNSMutableArray release];

You can read all about NSArrays at http://www.cocoadevcentral.com:
http://www.cocoadevcentral.com/articles/000006.php

They also have a ton of other great tutorials on objective-C and Cocoa. Here's a bunch of articles to get you started:
http://cocoadevcentral.com/articles/cat_...basics.php
Quote this message in a reply
Kaamoss
Unregistered
 
Post: #14
Thanks guys I appreciate all of the help. I'll be sure to give you guys a link to the source and app once it's done.
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #15
I'd also suggest you use NSArray/NSMutableArray for arrays when speed is not an issue, as in this case. Also, brush up on Cocoa's garbage collection mechanism & reference counting, as those are really important when working with Cocoa. Most of my mistakes relate to things not being retained/released at the right times.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  memory pointer tricks? Toontingy 2 4,201 Mar 31, 2009 02:35 AM
Last Post: Ingemar
  pointer cleanup questions kendric 7 4,623 Mar 29, 2009 07:48 PM
Last Post: kendric
  pointer or not? kensuguro 17 7,887 Aug 15, 2007 04:12 PM
Last Post: AnotherJake
  My AnimationManager didn't like being a pointer. milkfilk 6 3,931 Mar 24, 2007 10:03 PM
Last Post: unknown
  Pointer-related woes (C/C++) sealfin 2 2,697 Jan 18, 2006 01:22 PM
Last Post: sealfin