【问题标题】:Segmentation Fault : 11 Pointers and Structures分段错误:11个指针和结构
【发布时间】:2016-06-11 16:15:33
【问题描述】:

我在头文件中创建了两个结构,一个用于卡片,一个用于手,如下所示:

struct Card 
{                                 
    char *face; // define pointer face 
    char *suit; // define pointer suit
};


struct Hand
{
    struct Card *cards[15]; // define an array to hold a card
    char hand[13];  // defines the type of hand 
};

在我的printHand 函数中,我尝试为每手牌添加一张牌,然后将这些手牌添加到代表所有玩家的数组中。我运行了一个调试器,这里出现了分段错误:

// Here we will add the cards to the array holding the player's hands
totalHands[indexHand].cards[indexCard]->face = shuffledDeck[indexCard].face;
totalHands[indexHand].cards[indexCard]->suit = shuffledDeck[indexCard].suit;

调试器打印EXC_BAD_ACCESS。这是我的功能:

void printHand(struct Card* shuffledDeck, char* argv[])
{
    // total number of cards
    const int DeckMaxSize = 53;

    // get the command line input
    int numOfHands = atoi(argv[1]);
    int numOfCards = atoi(argv[2]);

    // counters for our loops
    int indexHand;
    int indexCard;

    struct Hand totalHands[10]; // an array holding all the hands

    // allocate memory for our struct
    struct Hand* tempHand = (struct Hand*)malloc(numOfCards * sizeof(struct Hand));

    // Adjusted the command line arguments if the number cards/hand and
    // players can not be resolved from a 52 sized deck
    int validateInput = DeckMaxSize / numOfHands;

    if (validateInput < numOfCards) {
        numOfCards = validateInput;
        printf("\n%s", "Input was adjusted to the size of the deck ( 52 )");
    }

    // Creates a new Hand and adds it to the totalHands[]
    for (indexHand = 1; indexHand <= numOfHands; indexHand++) {
        totalHands[indexHand] = *tempHand;
        printf("\n\n%s %d\n", "Player : ", indexHand);

        // add the cards to the hand and print each player's hand
        for (indexCard = 1; indexCard <= numOfCards; indexCard++) {
            // Here we will add the cards to the array holding the player's hands
            totalHands[indexHand].cards[indexCard]->face = shuffledDeck[indexCard].face;
            totalHands[indexHand].cards[indexCard]->suit = shuffledDeck[indexCard].suit;

            // Print all of our player's hand
            printf("\n%d %s ", indexCard, totalHands[indexHand].cards[indexCard]->face);
            printf("%s", totalHands[indexHand].cards[indexCard]->suit);
        }

        // print a new line for formatting
        printf("\n");
    }
}

【问题讨论】:

    标签: c pointers memory struct


    【解决方案1】:
    for(indexHand = 1; indexHand <= numOfHands; indexHand++){
    ...
    for(indexCard = 1; indexCard <= numOfCards; indexCard++){       
    

    这些循环从1n(包括最大索引)。但索引应该从 0n-1 。因此,这些循环访问索引越界。

    修改你的循环到这个 -

    for(indexHand = 0; indexHand < numOfHands; indexHand++){
    ...
    for(indexCard = 0; indexCard < numOfCards; indexCard++){       
    

    编辑 -

     totalHands[indexHand].cards[indexCard]->face = shuffledDeck[indexCard].face;
    totalHands[indexHand].cards[indexCard]->suit = shuffledDeck[indexCard].suit;
    

    在这两个语句之前,您没有将内存分配给指针数组cards,而是取消引用它(未初始化的指针)。

    分配这个数组的内存指针,然后访问数组成员。

    【讨论】:

    • 我也这么认为,但我仍然收到分段错误.. 总手数 (10) 的大小比我通过的输入 4 大得多。我还在struct size 15,但每个玩家最多 13 个。所以我真的不认为这是越界问题的索引。我相信这与指针或我访问它们的方式有关。
    • @LesterRamos 是的,也许是这样。我已经更新了答案。请看一下 。问题是你需要做的内存分配。
    • 啊,好吧,我认为当我为手形结构分配内存时会处理好。那么从语法上讲,我将如何访问 tempHand 结构中的卡片数组并为其分配内存?
    • 使用tempHand在循环中访问指向cards的每个指针并使用malloc为其分配内存。
    • 我在嵌套的 for 循环中这样做了,但仍然收到该死的分段错误。 tempHand->cards[indexCard] = (struct Card*) malloc(numOfCards * sizeof(struct Card));
    猜你喜欢
    • 2018-07-12
    • 1970-01-01
    • 1970-01-01
    • 2012-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-07
    相关资源
    最近更新 更多