I have two structs that looks like this
// Decks and cards
typedef struct _card{
int suit;
int value;
} card;
typedef struct _deck{
int num_of_decks;
int num_of_cards;
card* cards;
} deck;
the deck
struct represent a deck and card
represent a single card. So card* cards
represent a list of cards. Now I want to allocate memory for these structures.
card*
init_card(card* card, int suit, int value)
{
card->suit = suit;
card->value = value;
return card;
}
deck*
create_deck(int num_of_decks)
{
deck* deck = malloc(sizeof(deck));
int decks, suit, value;
size_t index = 0;
// Setup the decks to be stored
if(!deck)
{
fprintf(stderr, "blackJack: allocation error\n");
exit(EXIT_FAILURE);
}
deck->num_of_decks = num_of_decks;
deck->num_of_cards = num_of_decks*52;
deck->cards = malloc(deck->num_of_cards * sizeof(card*));
if(!deck->cards)
{
fprintf(stderr, "blackJack: allocation error\n");
free_data(deck);
exit(EXIT_FAILURE);
}
// Create the cards
index = 0;
for(decks = 0; decks < num_of_decks; decks++)
{
for(suit = 0; suit < 4; suit++)
{
for(value = 1; value <= 13; value++)
{
// Create a card and assign it to the deck
init_card(deck->cards + index, suit, value);
index++;
}
}
}
return deck;
}
Here what I trying to do is first of all allocate memory for the deck
struct, then allocate memory for all the cards. Then I place generate a card at each position while updating the index
value to place the next card in. However, when I check in valgrind I get the following error
==135923== Invalid write of size 8
==135923== at 0x10964E: create_deck (in /home/blackJack/blackJack)
==135923== by 0x1092A1: main (in /home/blackJack/blackJack)
==135923== Address 0x4a38048 is 0 bytes after a block of size 8 alloc'd
==135923== at 0x483A77F: malloc (vg_replace_malloc.c:307)
==135923== by 0x1095DE: create_deck (in /home/blackJack/blackJack)
==135923== by 0x1092A1: main (in /home/blackJack/blackJack)
==135923==
==135923== Invalid read of size 8
==135923== at 0x109656: create_deck (in /home/blackJack/blackJack)
==135923== by 0x1092A1: main (in /home/blackJack/blackJack)
==135923== Address 0x4a38048 is 0 bytes after a block of size 8 alloc'd
==135923== at 0x483A77F: malloc (vg_replace_malloc.c:307)
==135923== by 0x1095DE: create_deck (in /home/blackJack/blackJack)
==135923== by 0x1092A1: main (in /home/blackJack/blackJack)
==135923==
==135923== Invalid read of size 8
==135923== at 0x1096BC: create_deck (in /home/blackJack/blackJack)
==135923== by 0x1092A1: main (in /home/blackJack/blackJack)
==135923== Address 0x4a38048 is 0 bytes after a block of size 8 alloc'd
==135923== at 0x483A77F: malloc (vg_replace_malloc.c:307)
==135923== by 0x1095DE: create_deck (in /home/blackJack/blackJack)
==135923== by 0x1092A1: main (in /home/blackJack/blackJack)
==135923==
==135923== Invalid read of size 8
==135923== at 0x109250: free_data (in /home/blackJack/blackJack)
==135923== by 0x1092B1: main (in /home/blackJack/blackJack)
==135923== Address 0x4a38048 is 0 bytes after a block of size 8 alloc'd
==135923== at 0x483A77F: malloc (vg_replace_malloc.c:307)
==135923== by 0x1095DE: create_deck (in /home/blackJack/blackJack)
==135923== by 0x1092A1: main (in /home//blackJack/blackJack)
This as far as I have understood it is because when I increase the pointer it is not increased by the correct size, learned that from here. However, if I change sizeof(card*)
to sizeof(char*)
, I still get the exact same error. I have no idea of the cause since it in my mind is nothing wrong with what I have done.
Any help would be appreciated!