我正在为纸牌游戏编写一个C程序,而我目前正在制作算法来向每个玩家发牌。问题是,由于看似没有理由,我的一个数组指针在执行期间被设置为0,我无法弄清楚原因。它似乎发生在我的代码中的一个非常具体的位置。这是我声明数组的地方:
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;
}
接下来,我们跳过我制作一副纸牌并用卡片值填充它的部分,直到我在牌组中洗牌,准备好处理它们。似乎一旦shuffle结束,指向指针阵列的指针变坏了。
//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;
};
如果你想运行完整的应用程序,它是在https://github.com/Mikumiku747/kingandserf在Github上托管的C程序 除了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
即使有人不知道如何解决这个问题,他们至少可以告诉我发生了什么。我以前从未见过这个,我现在唯一的猜测是优化器以某种方式打破了它。
答案 0 :(得分:0)
循环中对swapCards
的第一次调用将索引54处的卡与另一个卡交换,但deck
数组中的最高索引为53。