未分配的C程序指针未分配

时间:2019-02-14 21:53:33

标签: c pointers realloc

我试图在某些条件下创建一个数组的两个子数组,并使用realloc增加指针空间。 但是,未分配此代码:显示的指针正在重新分配。 共有5个项目,第4个项目失败。我真的很困惑为什么它可以在前三个项目中成功。

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

1 个答案:

答案 0 :(得分:0)

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;
}
addCardsBySuit 中的

trumpCards 的值保持不变,因此下一次您将调用 addCardsBySuit 并尝试重新分配它将再次处理不再有效的旧值,而不是 realloc

的先前结果

您可以使用

进行更改
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);
  }
...