未分配正在重新分配的 C 程序指针

C Program pointer being realloc'd was not allocated

我试图通过一些条件创建一个数组的两个子数组,并使用realloc 来增加指针space。 然而,这个正在重新分配的 code:showed 指针没有分配。 总共有 5 个项目,这在第 4 个项目上失败了。我真的很困惑为什么它可以在前 3 项上成功。

void sortHand(Hand *hand, Suit trump) {
  int length = NUM_CARDS_IN_HAND - 1;
  Card *cards = getAllCardsFromHand(hand, length);
  Card *trumpCards = (Card*)malloc(sizeof(Card));
  Card *otherCards = (Card*)malloc(sizeof(Card));
  int trumpCount = 1, otherCount = 1;
  for (int i = 0; i <= length ; i++) {
    Card curtCard = cards[i];
    if (curtCard.suit == trump){
      trumpCount = addCardsBySuit(trumpCards, curtCard, trumpCount);
      printf("trumpCount %d\n", trumpCount);
      continue;
    }
      otherCount = addCardsBySuit(otherCards, curtCard, otherCount);
      printf("otherCount%d\n", otherCount);
  }
  if (trumpCards == NULL) {
    printf("Trump is zero. Other is %d.", (otherCount + 1));
    addAllCardsTohand(otherCards, hand, otherCount);
    return;
  }
  if (otherCards == NULL){
    printf("Other is zero. Trump is %d.", (trumpCount + 1));
    addAllCardsTohand(trumpCards, hand, trumpCount);
    return;
  }
  printf("Trump is %d. Other is %d.", (trumpCount + 1), (otherCount + 1));
  addAllCardsTohand(trumpCards, hand, trumpCount);
  addAllCardsTohand(otherCards, hand, otherCount);
}

我认为问题出在这个函数中。

int addCardsBySuit(Card *trumpCards, Card card, int trumpCount) {
  printf("addCardsBySuit\n");
  Card *moreCards = (Card*)realloc(trumpCards, trumpCount * sizeof(Card));
  trumpCards = moreCards;
  trumpCards[trumpCount] = card;
  trumpCount++;
  return trumpCount;
}

这是我的测试代码和输出。

void test_sort_hand() {
  start_test("sort_hand");
  Hand *hand = createHand();
  Card card1 = {NINE, HEARTS, -1};
  addCardToHand(&card1, hand);
  Card card2 = {JACK, HEARTS, -1};
  addCardToHand(&card2, hand);
  Card card3 = {ACE, HEARTS, -1};
  addCardToHand(&card3, hand);;
  Card card4 = {QUEEN, HEARTS, -1};
  addCardToHand(&card4, hand);;
  Card card5 = {TEN, SPADES, -1};
  addCardToHand(&card5, hand);
  printHand(hand);
  sortHand(hand, HEARTS);
  printHand(hand);
  end_test();
}

0: Ten_Spades
1: Queen_Hearts
2: Ace_Hearts
3: Jack_Hearts
4: Nine_Hearts
addCardsBySuit
otherCount2
addCardsBySuit
trumpCount 2
addCardsBySuit
trumpCount 3
addCardsBySuit
test(5155,0x11820d5c0) malloc: *** error for object 0x7fd811402b50: pointer being realloc'd was not allocated
test(5155,0x11820d5c0) malloc: *** set a breakpoint in malloc_error_break to debug
Abort trap: 6

int addCardsBySuit(Card *trumpCards, Card card, int trumpCount) {
  printf("addCardsBySuit\n");
  Card *moreCards = (Card*)realloc(trumpCards, trumpCount * sizeof(Card));
  trumpCards = moreCards;
  trumpCards[trumpCount] = card;
  trumpCount++;
  return trumpCount;
}

addCardsBySuittrumpCards 的值没有改变,所以下次你会调用 addCardsBySuit 并且您再次尝试 realloc 它,您将对不再有效的旧值执行操作,而不是 realloc[=13 的先前结果=]

您可以使用

进行更改
int addCardsBySuit(Card **trumpCards, Card card, int trumpCount) {
  printf("addCardsBySuit\n");
  Card *moreCards = (Card*)realloc(*trumpCards, trumpCount * sizeof(Card));
  *trumpCards = moreCards;
  (*trumpCards)[trumpCount] = card;
  trumpCount++;
  return trumpCount;
}

当然要更改调用以提供指针所在的地址:

void sortHand(Hand *hand, Suit trump) {
  int length = NUM_CARDS_IN_HAND - 1;
  Card *cards = getAllCardsFromHand(hand, length);
  Card *trumpCards = (Card*)malloc(sizeof(Card));
  Card *otherCards = (Card*)malloc(sizeof(Card));
  int trumpCount = 1, otherCount = 1;
  for (int i = 0; i <= length ; i++) {
    Card curtCard = cards[i];
    if (curtCard.suit == trump){
      trumpCount = addCardsBySuit(&trumpCards, curtCard, trumpCount);
      printf("trumpCount %d\n", trumpCount);
      continue;
    }
      otherCount = addCardsBySuit(&otherCards, curtCard, otherCount);
      printf("otherCount%d\n", otherCount);
  }
...