2015-09-21 65 views
0

我正在爲一款紙牌遊戲編寫一個C程序,而且我正在制定算法來爲每個玩家發牌。問題是,似乎沒有理由,我的數組指針在執行過程中被設置爲0,我無法弄清楚原因。它似乎發生在我的代碼中的一個非常特定的地方。這裏就是我聲明數組:我的C數組在執行期間被破壞

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

接下來,我們跳過,我做出一副撲克牌,並與卡值填充它,到了那裏,我洗牌在甲板上的點的部分,準備好處理它們。似乎只要洗牌結束,指向手陣列的指針就會變差。

//Then we add the jokers on the end 
struct Card blackJoker = {0, 13}; 
deck[52] = blackJoker; 
struct Card redJoker = {1, 13}; 
deck[53] = redJoker; 
//Now, we shuffle the deck using the Fisher Yates shuffle (the Durstenfeld version) 
for (int cardsleft = 54; cardsleft > 0; cardsleft--) { 
    swapCards(deck, cardsleft, rand() % cardsleft); 
} 
//IT SEEMS TO DROP THE HANDS ARRAY RIGHT HERE...run 
//Now that the cards are randomly shuffled, we just deal them out to 
// everyone (it doesn't matter about the order, this is supposed to 
//be truly random! 
currentCard = 0; 
for (int player = 0; player < leftovers; player++) { 
    //Deal an extra card for these players, since they take an extra 
    //card 
    for (int card = 0; card < hands[player].cardc; card++) { 
     hands[player].cardv[card] = deck[currentCard]; 
     currentCard++; 
    } 
} 

我沒有得到的東西是,直接在之前或之後的手陣列訪問。但仍然,手陣列結束了死亡,我得到一個Segfault試圖訪問一個瘋狂的地址。我將包含代碼的其他部分,以供參考。

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

這是我做存儲卡和手的結構:

struct Card { 
int suit; 
int value; 
}; 

struct Hand { 
    int cardc; 
    struct Card* cardv; 
}; 

如果你想運行完整的應用程序,它是託管在Github上一個C程序,在https://github.com/Mikumiku747/kingandserf 它不應該需要除C編譯器和make外的任何先決條件。至於一些決定性的證據,我把它通過調試,這是我發現那裏一切都錯了一點:

Reading symbols from /home/pi/kingandtheserf/bin/kats...done. 
(gdb) b cardops.c:90 
Breakpoint 1 at 0x8a58: file src/cardops.c, line 90. 
(gdb) b cardops.c:94 
Breakpoint 2 at 0x8aa8: file src/cardops.c, line 94. 
(gdb) b cardops.c:98 
Note: breakpoint 2 also set at pc 0x8aa8. 
Breakpoint 3 at 0x8aa8: file src/cardops.c, line 98. 
(gdb) run a a a 
Starting program: /home/pi/kingandtheserf/bin/kats a a a 

Breakpoint 1, dealHands (players=4, sort=0) at src/cardops.c:91 
91  for (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) at src/cardops.c:98 
98  currentCard = 0; 
(gdb) p hands 
$4 = (struct Hand *) 0x2 
(gdb) p hands[0] 
Cannot access memory at address 0x2 
(gdb) c 
Continuing. 

Program received signal SIGSEGV, Segmentation fault. 
0x00008b30 in dealHands (players=4, sort=0) at src/cardops.c:102 
102   for (int card = 0; card < hands[player].cardc; card++) { 
(gdb) quit 

即使有人不知道如何解決這個問題,可能他們至少告訴我發生了什麼事。我以前從未見過這種情況,現在我唯一的猜測是優化器以某種方式破壞了它。

+1

請張貼所有的變量聲明。看哪,我可以看到未來:這將成爲一個錯綜複雜的數組索引錯誤。 – Lundin

+0

好的,我添加了hands數組的聲明。感謝您查看它。如果您需要其他任何東西,我只會發布完整的標題和代碼,其中大約80%已經存在。 – Mikumiku747

回答

0

在循環中的第一次調用swapCards交換和另一個指標54卡,但指數最高到deck陣列爲53

+0

好了,爲了澄清,問題在於它寫過甲板陣列的末尾,並進入我的指針到hands數組?我想我現在已經明白了,謝謝你的幫助。你確實叫它@Lundin,但真的,謝謝你的幫助,我一直在看這個30分鐘,沒有任何想法。 :) – Mikumiku747