Random pointer issues - Printable Version
+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Game Programming Fundamentals (/forum-7.html)
+--- Thread: Random pointer issues (/thread-5538.html)
Pages: 1 2
Random pointer issues - Kaamoss - May 9, 2005 02:35 PM
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.
Random pointer issues - Blorx2 - May 9, 2005 02:41 PM
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.
[OFF-TOPIC](What a coincidence, my name is Matt too...)[/OFF-TOPIC]
Random pointer issues - Kaamoss - May 9, 2005 02:58 PM
Awesome I would appreciate any help, here is the link. Code Listing
Random pointer issues - longjumper - May 9, 2005 03:11 PM
I don't know anything about Objective-C, but you're problem may be in that you declare the Deck as Deck * cards. 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, 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.
Random pointer issues - Blorx2 - May 9, 2005 03:16 PM
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...
Random pointer issues - longjumper - May 9, 2005 03:19 PM
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.
Random pointer issues - Kaamoss - May 9, 2005 03:24 PM
I tried re declaring Card * deck to deck card which gives me a parsing error. I assume the method which you [LongJumper] is flawed as I should have to declair deck 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.
Random pointer issues - longjumper - May 9, 2005 03:26 PM
Whoops, my bad. You should be saying Card deck or Card* deck. An array of 52 cards, yup, my mistake, I just went over it quickly.
Random pointer issues - Kaamoss - May 9, 2005 03:31 PM
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 to Card deck 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.
Random pointer issues - Kaamoss - May 9, 2005 04:44 PM
ok so I fixed it here's how.
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.
Random pointer issues - longjumper - May 9, 2005 06:50 PM
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.
Random pointer issues - wadesworld - May 9, 2005 07:35 PM
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.
Random pointer issues - Andrew - May 9, 2005 10:43 PM
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...
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:
You can read all about NSArrays at http://www.cocoadevcentral.com:
They also have a ton of other great tutorials on objective-C and Cocoa. Here's a bunch of articles to get you started:
Random pointer issues - Kaamoss - May 9, 2005 11:51 PM
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.
Random pointer issues - DoG - May 10, 2005 01:22 AM
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.