My C array gets corrupted during execution -


i'm writing c program card game, , i'm making algorithm deal cards each of players. problem is, seemingly no reason, 1 of array pointers getting set 0 during execution, , can't figure out why. seems happen @ specific spot in code. here declare array:

struct hand *hands = (struct hand*)calloc(players, sizeof(struct hand)); (int player = 0; player < leftovers; player++) {     //allocate spot these players, since take     //extra card     hands[player].cardv = (struct card*)calloc((cardsperplayer + 1), sizeof(struct card));     hands[player].cardc = cardsperplayer + 1; } (int player = leftovers; player < players; player++) {     //allocate normal amount of cards rest of players     hands[player].cardv = (struct card*)calloc((cardsperplayer), sizeof(struct card));     hands[player].cardc = cardsperplayer; } 

next, skip past part make deck of cards , fill cards values, point i'm shuffling cards in deck, ready deal them. seems shuffle finishes, pointer hands array goes bad.

//then add jokers on end struct card blackjoker = {0, 13}; deck[52] = blackjoker; struct card redjoker = {1, 13}; deck[53] = redjoker; //now, shuffle deck using fisher yates shuffle (the durstenfeld version) (int cardsleft = 54; cardsleft > 0; cardsleft--) {     swapcards(deck, cardsleft, rand() % cardsleft); } //it seems drop hands array right here...run  //now cards randomly shuffled, deal them out // (it doesn't matter order, supposed //be random! currentcard = 0; (int player = 0; player < leftovers; player++) {     //deal card these players, since take     //card     (int card = 0; card < hands[player].cardc; card++) {         hands[player].cardv[card] = deck[currentcard];         currentcard++;     } } 

the thing don't is, @ no point directly before or after hands array accessed. still, hands array ends dying , segfault trying access crazy address. i'll include of other parts of code reference.

void swapcards(struct card *set, int a, int b) { struct card temp = set[a]; set[a] = set[b]; set[b] = temp; 

here's structures made store cards , hands:

struct card { int suit; int value; };  struct hand {     int cardc;     struct card* cardv; }; 

if want run full application, it's c program hosted on github, @ https://github.com/mikumiku747/kingandserf shouldn't need prerequisites apart c compiler , make. final proof, put through debugger, how found point goes wrong:

reading symbols /home/pi/kingandtheserf/bin/kats...done. (gdb) b cardops.c:90 breakpoint 1 @ 0x8a58: file src/cardops.c, line 90. (gdb) b cardops.c:94 breakpoint 2 @ 0x8aa8: file src/cardops.c, line 94. (gdb) b cardops.c:98 note: breakpoint 2 set @ pc 0x8aa8. breakpoint 3 @ 0x8aa8: file src/cardops.c, line 98. (gdb) run a starting program: /home/pi/kingandtheserf/bin/kats a  breakpoint 1, dealhands (players=4, sort=0) @ src/cardops.c:91 91      (int cardsleft = 54; cardsleft > 0; cardsleft--) { (gdb) p hands $1 = (struct hand *) 0x13008 (gdb) p hands[0] $2 = {cardc = 14, cardv = 0x13030} (gdb) p hands[2] $3 = {cardc = 13, cardv = 0x13120} (gdb) c continuing.  breakpoint 2, dealhands (players=4, sort=0) @ src/cardops.c:98 98      currentcard = 0; (gdb) p hands $4 = (struct hand *) 0x2 (gdb) p hands[0] cannot access memory @ address 0x2 (gdb) c continuing.  program received signal sigsegv, segmentation fault. 0x00008b30 in dealhands (players=4, sort=0) @ src/cardops.c:102 102         (int card = 0; card < hands[player].cardc; card++) { (gdb) quit 

even if doesn't know how fix this, @ least tell me what's going on. i've never seen before, , guess right optimizer broke somehow.

the first call swapcards in loop swaps card @ index 54 another, highest index deck array 53.


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -