初始化指针数组

发布于 2024-12-07 12:35:18 字数 1065 浏览 0 评论 0原文

我有一个 Deck 对象(一副牌),它是一个作为双向链表实现的双端队列。我希望能够随意调整队列,但我无法做到这一点。因此,我选择预先洗牌一个指向卡片的数组,并在事后将它们排入队列。问题是,我现在的代码似乎根本没有初始化指针。

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++) {
        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]);
}

我认为我搞砸的地方是在 card[i] = new Card(...) 行中,但它在某种程度上对我来说看起来是正确的。

任何建议将不胜感激。

免责声明:我知道我应该使用标准库来处理大部分内容,但我试图首先自学困难的内容。这就是我学习的方式。

编辑:我解决了索引问题。现在我只想弄清楚为什么有些图像现在没有绘制...:/感谢您的帮助!

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++) {
        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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最好是你 2024-12-14 12:35:18

您的代码有很多问题

  1. 您正在使用 1 <= i <= 20 进行循环,但对于 20 个元素的数组,索引来自 0 <= index <= 19。您需要使用 cards[i-1] = new Card(i,path);

  2. 您正在分配指针数组 cards 但您没有取消分配它(内存泄漏)。完成后,可以使用 delete[] cards; 取消分配它,或者仅使用 Card *cards[20]; 基于堆栈的数组,而不是使用 分配它new

  3. 您计算 MAX_INTEGER_LENGTH 的方式表明您并不真正了解 sizeof 的作用。

  4. 这就是牌不被洗牌的原因。您编写了一个交换两个指针的函数,但它交换的指针是函数的局部变量(参数),而不是数组的元素。一种解决方案是通过用 void Swap(Card *& a, Card *& b) 声明 swap 将参数作为指针引用传递,另一种解决方案是传递指针到指针(但是由于双重间接寻址,这将需要更复杂的实现语法,并且还需要更改调用函数的方式)。

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).

泡沫很甜 2024-12-14 12:35:18

在第一个 for 循环中,起始索引为 0,而在第二个 for 循环中,起始索引为 0。这可能是问题所在。

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.

一绘本一梦想 2024-12-14 12:35:18

您的代码:

 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);
    }

这里循环应该从 0 开始到 20 如下:

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

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

修复后,您可以使用 i+1 而不是 >i in :

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

如果需要的话。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文