0

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!

page290
  • 39
  • 5
  • 2
    It's not a good idea to use the same name for a type and a variable. – Scott Hunter Dec 27 '20 at 03:04
  • Ok, will try and change the name. Do you believe that may be what cause the error? – page290 Dec 27 '20 at 03:07
  • 1
    Did you read the error? sizeof(deck) is 8, which is the size of a pointer. You should have used `sizeof(*deck)` – stark Dec 27 '20 at 03:08
  • 5
    You want `sizeof(card)`, not `sizeof(card*)`. Or use the variable you're assigning to, `sizeof(*deck->cards)`. – 1201ProgramAlarm Dec 27 '20 at 03:09
  • @ScottHunter This was the problem, now everything works perfectly! Thank you! In responce to the rest, I have tried all of them also, but the problem remained (probably because of the type/variable error). Thus I have just bruteforced without thinking about it. Ty! – page290 Dec 27 '20 at 03:14
  • You should be getting line numbers in the reports. As you're not getting them, you need to recompile the object files with `-g` and also link with `-g` among the options. That makes it easier to debug the issues. – Jonathan Leffler Dec 27 '20 at 04:02

0 Answers0