gpt4 book ai didi

c++ - Visual Studio 抛出异常 : write acces violation. q_deck->p_deck 为 0x110112。 C语言

转载 作者:行者123 更新时间:2023-11-30 16:09:20 25 4
gpt4 key购买 nike

我遇到了奇怪的错误,并且不知道该怎么办。我试图找到解决方案,但找不到适合我的解决方案。这是纸牌游戏,当我尝试将纸牌添加到我的列表(即一副纸牌)时,会显示错误。这是我的完整代码,以充分理解我的想法。它是用 c 编写的,但我使用的是 c++ 中的 cin/cout,这就是为什么我有“使用命名空间 std;”谢谢您的帮助:)

#define SIZE 52
#define HALF_SIZE 26

using namespace std;

typedef enum Suit { Diamonds, Hearts, Spades, Clubs } Suit_t;
typedef enum Power { Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, Ace } Power_t;

typedef struct card {
Suit_t suit;
Power_t power;
}cards;

typedef struct deck {
cards* p_deck;
int size;
}decks;

void Deck_Creator(cards* deck) {
for (int i = 0; i < SIZE; i++) {
deck[i].suit = (Suit)(i / 13);
deck[i].power = (Power)(i % 13);
}

}

void Display_Deck(cards* deck) {
for (int i = 0; i < SIZE; i++) {

if (deck[i].suit == 0) {
cout << "Diamond ";
}
else if (deck[i].suit == 1) {
cout << "Heart ";
}
else if (deck[i].suit == 2) {
cout << "Spade ";
}
else {
cout << "Club ";
}
cout << " " << deck[i].power << endl;
cout << endl;
}
}

void Deck_Shuffler(cards* deck) {
int changer = 0; // Index of changed card
cards temp = {}; // Temporary holding place for changing
srand(time(NULL)); // Function to get random numbers with current time
for (int i = 0; i < SIZE; i++) {
changer = rand() % SIZE; // Generating random number from 0 to 51
temp = deck[i];
deck[i] = deck[changer]; // if changer = i
deck[changer] = temp;
}
}

void Deck1_Creator(cards* deck, cards* deck1, decks* q_deck1) {
for (int i = 0; i < HALF_SIZE; i++) {
memcpy(&deck1[i], &deck[i], sizeof(cards)); // Copying content using memcpy function
}
q_deck1->p_deck = deck1;
q_deck1->size = HALF_SIZE;
}

void Deck2_Creator(cards* deck, cards* deck2, decks* q_deck2) {
int i = HALF_SIZE;
for (int i = 26; i < SIZE; i++) {
deck2[i - HALF_SIZE] = deck[i]; // Simple copying struct array content into another array
}
q_deck2->p_deck = deck2;
q_deck2->size = HALF_SIZE;
}

void table_creator(decks* q_deck) {
q_deck->p_deck = {};
q_deck->size = 0;
}

void enqueue(decks* q_deck, cards card) {
// I TRIED THAT TOO
//int n;
//n = q_deck->size;
q_deck->p_deck[q_deck->size] = card;
q_deck->size++;
}

cards* dequeue(decks* q_deck) {
cards tmp_card;

tmp_card = q_deck->p_deck[0];
for (int i = 0; i < q_deck->size - 1; i++) {
q_deck->p_deck[i] = q_deck->p_deck[i + 1];
}
q_deck->size--;

return &tmp_card;
}

void disp_queue(decks* q_deck) {
for (int i = 0; i < q_deck->size; i++) {
cout << q_deck->p_deck[i].suit <<" ";
cout << q_deck->p_deck[i].power << endl;
}
}

int main() {
cards deck[SIZE];
cards deck1[SIZE];
cards deck2[SIZE];
cards table1[HALF_SIZE];
cards table2[HALF_SIZE];
decks q_deck1;
decks q_deck2;
decks q_table1;
decks q_table2;
char choice;

table_creator(&q_table1);
table_creator(&q_table2);

for(; ;) { //Never ending loop
cin >> choice;
switch (choice) {
case '1':
cout << endl << "Unshuffled deck: " << endl;
Deck_Creator(deck);
Display_Deck(deck);
break;
case '2':
cout << endl << "Shuffled deck: " << endl;
Deck_Shuffler(deck);
Display_Deck(deck);
break;
case '3':
cout << endl << "Hand 1:" << endl;
Deck1_Creator(deck, deck1, &q_deck1);
disp_queue(&q_deck1);
break;
case '4':
cout << endl << "Hand 2:" << endl;
Deck2_Creator(deck, deck2, &q_deck2);
disp_queue(&q_deck2);
break;
case '5':
cards tmp_card = *dequeue(&q_deck1);
enqueue(&q_table1, tmp_card);
tmp_card = *dequeue(&q_deck2);
enqueue(&q_table2, tmp_card);
disp_queue(&q_table1);
disp_queue(&q_table2);
break;
case '6':
disp_queue(&q_deck1);
break;
}
}
return 0;
}

ERROR 仅当我在 switch 函数中按下 case 5 时才会弹出。其余的都运作良好。此函数中出现错误:

cards* dequeue(decks* q_deck) {
cards tmp_card;

tmp_card = q_deck->p_deck[0]; // EXACTLY IN THIS LINE RED X OCCURS
for (int i = 0; i < q_deck->size - 1; i++) {
q_deck->p_deck[i] = q_deck->p_deck[i + 1];
}
q_deck->size--;

return &tmp_card;
}

请帮助我理解这个错误,以便我完全知道为什么以及这个错误是如何工作的:(谢谢您的宝贵时间,我知道代码很长。

最佳答案

Yano 关于分配给 p_d​​eck 的内存不足的说法是正确的。我有点惊讶其他人竟然在工作(你似乎根本没有为这里的卡片分配任何空间,相反,你只是将卡片值写入指针旁边的区域,这在技术上并不重要属于您的指针,并且不保证保持不变)。

由于您使用的是 C++,因此您可以通过向处理分配内存的结构添加构造函数/析构函数来解决此问题。

typedef struct deck {
cards* p_deck;
int size;

//constructor
deck() : size(SIZE) //this sets the size to your size constant
{
p_deck = malloc(size * sizeof(*p_deck)); //this gives p_deck memory space
}

//destructor
~deck()
{
if(p_deck) delete[] p_deck; //this clears up the memory assigned to p_deck
p_deck = nullptr;
}

}deck; //edited so it matches the actual struct name

要使用构造函数,请在创建对象时使用 deck varName = Deck(); 而不是 deck varName;

当对象超出范围时(在 main() 或其本地函数的末尾),会自动调用析构函数。如果您出于某种原因想要释放内存(例如,如果您想重新创建一个全新的牌组),也可以使用 varName.~deck();

手动调用它

关于c++ - Visual Studio 抛出异常 : write acces violation. q_deck->p_deck 为 0x110112。 C语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59167250/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com