I tried to create two sub array of one array by some conditions and use realloc to increase the pointer space. However, this code:showed pointer being realloc'd was not allocated. There are totally 5 items and this failed on 4th items. I am really confused why it can succeed for first 3 items.
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);
}
I think problem is in this function.
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;
}
This is my test code and output.
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
In
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;
}
out of addCardsBySuit the value of trumpCards is unchanged, so the next time you will call addCardsBySuit and you try to realloc it again you will do on the old value not valid anymore rather than the previous result of realloc
You can change with
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;
}
of course changing the call to give the address where the pointer is :
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);
}
...