Random pointer issues

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.

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. 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.

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.

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...

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...

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.

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.

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.

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.

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.

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:

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.