初始化指针数组
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的代码有很多问题
您正在使用
1 <= i <= 20
进行循环,但对于 20 个元素的数组,索引来自0 <= index <= 19
。您需要使用cards[i-1] = new Card(i,path);
您正在分配指针数组
cards
但您没有取消分配它(内存泄漏)。完成后,可以使用delete[] cards;
取消分配它,或者仅使用Card *cards[20];
基于堆栈的数组,而不是使用分配它new
。您计算
MAX_INTEGER_LENGTH
的方式表明您并不真正了解sizeof
的作用。这就是牌不被洗牌的原因。您编写了一个交换两个指针的函数,但它交换的指针是函数的局部变量(参数),而不是数组的元素。一种解决方案是通过用
void Swap(Card *& a, Card *& b)
声明 swap 将参数作为指针引用传递,另一种解决方案是传递指针到指针(但是由于双重间接寻址,这将需要更复杂的实现语法,并且还需要更改调用函数的方式)。Your code has many problems
You are looping with
1 <= i <= 20
but for an array of 20 elements indexing goes from0 <= index <= 19
. You need to usecards[i-1] = new Card(i,path);
You are allocating the array of pointers
cards
but you are not deallocating it (memory leak). Either deallocate it withdelete[] cards;
once you are done or just use a stack based array withCard *cards[20];
instead of allocating it withnew
.The way you compute
MAX_INTEGER_LENGTH
shows you don't really understand whatsizeof
does.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).在第一个 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.
您的代码:
这里循环应该从
0
开始到20
如下:修复后,您可以使用
i+1
而不是>i
in :如果需要的话。
Your code:
Here the loop should start from
0
to20
as:And after the fix, you could use
i+1
instead ofi
in :if that is required.