开发者

Initializing array of pointers

I have a Deck object (deck of cards) which is a double-ended queue implemented as a doubly-linked list. I would like to be able to shuffle the queue at will, but the way I would go about it is beyond me. So instead I've opted to pre-shuffle an array a pointers to the cards and enqueue them after the fact. Problem is, the code I have now doesn't seem to be initializing the pointers at all.

void BuildDeck(Deck* deck) {
    Card** cards = new Card*[20];
    const size_t MAX_INTEGER_LENGTH = sizeof(int) * 4;
    char szPostfix[] = "_Card.bmp"; 

    for(int i = 1; i < 21; i++) {
   开发者_JAVA百科     char path[MAX_INTEGER_LENGTH + sizeof(szPostfix) + 1];
        sprintf(path,"%d%s",i, szPostfix);
        cards[i-1] = new Card(i,path);
    }
    ShuffleArray(cards);
    for (int i = 0; i < 20; i++) {
        deck->PushTop(cards[i]);
    }
}

void Swap(Card* a, Card* b) {
    Card temp = *a;
    *a = *b;
    *b = temp;
}

void ShuffleArray(Card** cardArray) {
    srand(dbTimer());
    for (int i = 0; i < 20; i++)
        Swap(cardArray[i],cardArray[rand()%20]);
}

I think where I screwed up is in the card[i] = new Card(...) line, but it somehow looks right to me.

Any suggestions would be appreciated.

DISCLAIMER: I know I should be using the standard library for most of this stuff, but I'm trying to teach myself the hard stuff first. It's just the way I learn.

EDIT: I fixed the index problem. Now I've just gotta figure out why some image aren't drawing now... :/ Thanks for the help!


Your code has many problems

  1. You are looping with 1 <= i <= 20 but for an array of 20 elements indexing goes from 0 <= index <= 19. You need to use cards[i-1] = new Card(i,path);

  2. You are allocating the array of pointers cards but you are not deallocating it (memory leak). Either deallocate it with delete[] cards; once you are done or just use a stack based array with Card *cards[20]; instead of allocating it with new.

  3. The way you compute MAX_INTEGER_LENGTH shows you don't really understand what sizeof does.

  4. This is the reason for which the cards don't get shuffled. You wrote a function that swaps two pointers, but the pointers it is swapping are local variables (parameters) of the function, not the elements of the array. One solution is to pass the parameters as pointer references by declaring swap with void Swap(Card *& a, Card *& b), another solution would be passing pointers to pointers (but this would require a more complex syntax of the implementation because of the double indirection and would also require a change in the way you call the function).


In the first for loop your starting index is 0, while in the second for loop the starting index is 0. That could be the problem.


Your code:

 for(int i = 1; i < 21; i++) {
        char path[MAX_INTEGER_LENGTH + sizeof(szPostfix) + 1];
        sprintf(path,"%d%s",i, szPostfix);
        cards[i] = new Card(i,path);
    }

Here the loop should start from 0 to 20 as:

 for(int i = 1 ; i < 21; i++) //incorrect - original code

 for(int i = 0 ; i < 20; i++) //correct - fix

And after the fix, you could use i+1 instead of i in :

        sprintf(path,"%d%s",i+1, szPostfix);
        cards[i] = new Card(i+1,path);

if that is required.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜