The ILP style of naming works better since some of the above is not necessarily true.
Printable View
OK here is some output when trying to test values of the pointers:
Well clearly something is going on here. I have had enough of it tonight, I will try and debug it tomorrow. Thanks for your help matsp.Code:Your Cards: king of hearts , ace of spades ; Score: 21
Computer's Cards: ace of spades, queen of clubs ; Score: 21
Would you like another card? [y,n]n
Computer is now deciding..
You: 21
Computer: 21
Same score, nobody won!
After first game:
(allcards+0)->card = 12; (mycards+0)->card = 12; (compscards+0)->card = 0
(allcards+0)->suit = 2; (mycards+0)->suit = 2; (compscards+0)->suit = 0
(allcards+0)->cleartext = ; (mycards+0)->cleartext = king of hearts; (compscards+0)->cleartext = ace of spades
(allcards+1)->card = 0; (mycards+1)->card = 0; (compscards+1)->card = 11
(allcards+1)->suit = 0; (mycards+1)->suit = 0; (compscards+1)->suit = 1
(allcards+1)->cleartext = ; (mycards+1)->cleartext = ace of spades; (compscards+1)->cleartext = queen of clubs
(allcards+2)->card = 0; (mycards+2)->card = 0; (compscards+2)->card = 0
(allcards+2)->suit = 0; (mycards+2)->suit = 0; (compscards+2)->suit = 0
(allcards+2)->cleartext = �; (mycards+2)->cleartext = ; (compscards+2)->cleartext =
(allcards+3)->card = 11; (mycards+3)->card = 0; (compscards+3)->card = 0
(allcards+3)->suit = 1; (mycards+3)->suit = 0; (compscards+3)->suit = 0
(allcards+3)->cleartext = ; (mycards+3)->cleartext = ; (compscards+3)->cleartext =
(allcards+4)->card = 11; (mycards+4)->card = 0; (compscards+4)->card = 0
(allcards+4)->suit = 0; (mycards+4)->suit = 0; (compscards+4)->suit = 0
(allcards+4)->cleartext = ; (mycards+4)->cleartext = ; (compscards+4)->cleartext =
Your Cards: king of diamonds , nine of spades ; Score: 19
Computer's Cards: nine of clubs, king of spades ; Score: 19
Would you like another card? [y,n]y
nine of hearts
Current Score: 28
Computer is now deciding..
You: 28
Computer: 19
You lost. Unlucky
After 2nd game:
(allcards+0)->card = 11; (mycards+0)->card = 0; (compscards+0)->card = 0
(allcards+0)->suit = 2; (mycards+0)->suit = 0; (compscards+0)->suit = 0
(allcards+0)->cleartext = ; (mycards+0)->cleartext = ; (compscards+0)->cleartext =
(allcards+1)->card = 11; (mycards+1)->card = 0; (compscards+1)->card = 0
(allcards+1)->suit = 2; (mycards+1)->suit = 0; (compscards+1)->suit = 0
(allcards+1)->cleartext = ; (mycards+1)->cleartext = ; (compscards+1)->cleartext =
(allcards+2)->card = 11; (mycards+2)->card = 0; (compscards+2)->card = 0
(allcards+2)->suit = 2; (mycards+2)->suit = 0; (compscards+2)->suit = 0
(allcards+2)->cleartext = ; (mycards+2)->cleartext = ; (compscards+2)->cleartext =
(allcards+3)->card = 11; (mycards+3)->card = 0; (compscards+3)->card = 0
(allcards+3)->suit = 2; (mycards+3)->suit = 0; (compscards+3)->suit = 0
(allcards+3)->cleartext = ; (mycards+3)->cleartext = ; (compscards+3)->cleartext =
(allcards+4)->card = 11; (mycards+4)->card = 0; (compscards+4)->card = 0
(allcards+4)->suit = 2; (mycards+4)->suit = 0; (compscards+4)->suit = 0
(allcards+4)->cleartext = ; (mycards+4)->cleartext = ; (compscards+4)->cleartext =
Your Cards: eight of diamonds , king of clubs ; Score: 18
Computer's Cards: king of spades, ace of spades ; Score: 21
Would you like another card? [y,n]y
nine of spades
Current Score: 27
Computer is now deciding..
You: 27
Computer: 21
You lost. Unlucky
*** glibc detected *** double free or corruption (!prev): 0x0804b008 ***
Aborted
Printing what the pointer points at is a good thing, but I actually meant something like this:
--Code:printf("allcards=%p, ...", allcards, ...);
Mats
Another thought: Add an extra element at the end of each dynamically allocated structure (those that you call malloc to get memory for). This extra element is filled with some unique and distinct data values (0xdeadbeef, 0xfeedbabe or 0xbabebabe for example). You then add a function and macro like this:
The above code need some tweaking, it's just to describe the concept. Using a macro to get the function, line and file is quite useful to know which of your sprinklings of CheckAllCards() was the one that triggered. You should really sprinkle those about - at the very least at the start and end of each function.Code:#define CheckAllCards() XCheckAllCards(allcards, __function__, __LINE__, __FILE__)
void XCheckAllCards(struct card_t *allcards, const char *func, int line, const char *file)
{
if (allcards[maxindex+1] .something != yourcheckvalue) {
printf("Help, someone clobbered the end of allcards\n"
"Called from %s:%d:%s\n",
func, line, file);
}
}
You obviously need to add some code to set this last element to the special check value, and add one extra on the malloc calls to make space for it.
You may want to check ALL the structures at once, or use separate macros for each - I don't know which will work best for you.
The idea here, as you probably understand, is to use the extra element at the end as a "marker", so that when it gets overwritten, you know something has gone wrong.
--
Mats
ok, i didnt really understand your code but i understood what you meant. So I added an extra one to malloc:
I added Check() at the beginning and end of startgame() and they didn't differ. Actually, since adding this extra code, I havn't had the error. This is weird.Code:
#define Check() check(allcards, mycards, compscards)
struct card_t {
unsigned short int card; /* Ace-King (1-13) */
unsigned short int suit; /* Clubs,Diamonds,Hearts,Spades (1-4) */
char cleartext[20]; /* "ace of spades" */
int something;
};
....
allcards = malloc( sizeof( *allcards ) * (NUMOFCARDS+1) );
mycards = malloc( sizeof( *mycards ) * (MAXINHAND+1) );
compscards = malloc( sizeof( *compscards ) * (MAXINHAND+1) );
allcards[(sizeof( *allcards ) * NUMOFCARDS)].something = 0xdeadbeef;
mycards[(sizeof( *mycards ) * NUMOFCARDS)].something = 0xfeedbabe;
compscards[(sizeof( *compscards ) * NUMOFCARDS)].something = 0xbabebabe;
...............
void check( struct card_t *allcards, struct card_t *mycards, struct card_t *compscards ) {
printf("%x,%x, %x\n", allcards[(sizeof( *allcards ) * NUMOFCARDS)].something,
mycards[(sizeof( *mycards ) * NUMOFCARDS)].something,
compscards[(sizeof( *compscards ) * NUMOFCARDS)].something
);
}
Edit: Wait, why would 'something' be overwritten? I've completely misunderstood what you meant havn't I? Did you mean: add another element to the end of the array, fill it with distinct values and see if it is overwritten at certain stages in the program? I think you did, I just didn't see the purpose of the 0xdeadbeef thing.
sizeof is only needed to be used when you use malloc:Code:
#define Check() check(allcards, mycards, compscards)
struct card_t {
unsigned short int card; /* Ace-King (1-13) */
unsigned short int suit; /* Clubs,Diamonds,Hearts,Spades (1-4) */
char cleartext[20]; /* "ace of spades" */
int something;
};
....
allcards = malloc( sizeof( *allcards ) * (NUMOFCARDS+1) );
mycards = malloc( sizeof( *mycards ) * (MAXINHAND+1) );
compscards = malloc( sizeof( *compscards ) * (MAXINHAND+1) );
allcards[NUMOFCARDS].something = 0xdeadbeef;
mycards[NUMOFCARDS].something = 0xfeedbabe;
compscards[NUMOFCARDS].something = 0xbabebabe;
...............
void check( struct card_t *allcards, struct card_t *mycards, struct card_t *compscards ) {
printf("%x,%x, %x\n", allcards[NUMOFCARDS].something,
mycards[NUMOFCARDS].something,
compscards[NUMOFCARDS].something
);
}
exactly.Quote:
Edit: Wait, why would 'something' be overwritten? I've completely misunderstood what you meant havn't I? Did you mean: add another element to the end of the array, fill it with distinct values and see if it is overwritten at certain stages in the program?
it's just a fun hexadecimal number to use.Quote:
I think you did, I just didn't see the purpose of the 0xdeadbeef thing.
Thanks Robwhit, that was very silly of me. Yes I see what they are I just don't know where to use them. I dont see how adding an extra struct element and setting those values at the end of the array makes a difference when I am not using that part of the struct throughout my program.
It's silly spending this much time dwelling on the error. I think it's time for me to just let it go, the program is nothing special anyway. Thanks to matsp and robwhit.
Still, save that source.
At some time, you will come back when you are good enough, and be able to fix it.
Solved.
Here is the culprit:
This picks a random number between 26 and 52. Now what if x = 52? And there's the problem. We can't access deck[52]. That is why it started working when I added 1 to the malloc.Code:x = ( rand() % ( NUMOFCARDS - ( NUMOFCARDS / 2 ) + 1 ) + ( NUMOFCARDS / 2 ) );