堆栈引发内存访问冲突
Stack throws memory access violation
所以我正在尝试创建一个简单的扑克游戏来重新熟悉C++,因为我已经离开它一段时间了,想回到C++。
我在 STL 的堆栈(适当,不是吗?我似乎无法让它允许我向堆栈添加任何东西。每次我在堆栈对象上调用 push 函数时,它都会引发内存访问冲突......我什至尝试创建一个整数堆栈,并将其添加到堆栈中,但它仍然抛出内存访问冲突错误。这很奇怪。
当前将对象添加到堆栈的代码为:
//shuffles the deck
void Deck::shuffle()
{
int index;
for(int i = 0; i < DECK_SIZE; i++)
{
index = (rand() % 52);
if(deck[index].drawn == false)
{
shuffledDeck.push(deck[index]); //throws the error
deck[index].drawn = true;
}
else
{
i--;
}
}
}
我设置了一个断点,然后沿着兔子洞跟踪它,发现它在这里抛出异常:
void push_back(const value_type& _Val)
{ // insert element at end
this->_Orphan_all();
_PUSH_BACK_BEGIN; //EXCEPTION!
this->_Getal().construct(
this->_Map[_Block] + _Newoff % _DEQUESIZ, _Val);
_PUSH_BACK_END;
}
位于德克.cpp...这很奇怪。
作为参考,以下是我认为可能需要的其余代码:
甲板.cpp
#include "stdafx.h"
#include "Deck.h"
#include <time.h>
using namespace std;
using namespace CardDeck;
const int DECK_SIZE = 52;
CARD deck[DECK_SIZE];
std::stack<CARD> shuffledDeck;
Deck::Deck()
{
srand((unsigned int)time(NULL));
loadCards();
}
Deck::~Deck()
{
}
//Draws a card from the deck
CARD Deck::drawCard()
{
CARD card = shuffledDeck.front();
shuffledDeck.pop();
return card;
}
//shuffles the deck
void Deck::shuffle()
{
int index;
for(int i = 0; i < DECK_SIZE; i++)
{
index = (rand() % 52);
if(deck[index].drawn == false)
{
shuffledDeck.push(deck[index]);
deck[index].drawn = true;
}
else
{
i--;
}
}
}
void Deck::loadCards()
{
//create input file stream
ifstream cardList;
//open the list of cards
cardList.open("card.txt", ios::in);
//if the file has opened successfully
if(cardList.is_open())
{
int index = 0;
//and while the cardlist has data left
while(cardList.good())
{
char * context = NULL;
CARD card = CARD();
//read the cards
string line;
getline(cardList, line);
char * s = strtok_s(&line[0], ", ", &context);
card.value = *s;
s = strtok_s(NULL, ", ", &context);
card.suit = s;
card.drawn = false;
deck[index] = card;
index++;
}
}
}
Deck.h (包含 CARD 结构)
#pragma once
#include <string>
#include <stack>
#include <fstream>
#include <sstream>
#include <iostream>
#include <string>
namespace CardDeck
{
struct CARD
{
public:
std::string suit;
char value;
bool drawn;
CARD(){}
CARD(const CARD& card)
{
suit = card.suit;
value = card.value;
drawn = card.drawn;
}
CARD& operator= (const CARD& card)
{
suit = card.suit;
value = card.value;
drawn = card.drawn;
return *this;
}
};
class Deck
{
public:
Deck();
~Deck();
Deck(const Deck& deck);
CARD drawCard();
void shuffle();
private:
void loadCards();
};
}
你可能在loadCards()
中写过deck
数组的末尾,破坏内存。
即使您正好有 52 行数据,cardlist.good()
也会返回true
,直到EOF
指示之后。
您可能想尝试以下循环(未经测试):
string line;
while(getline(cardList, line) && (index < 52))
{
char * context = NULL;
CARD card = CARD();
char * s = strtok_s(&line[0], ", ", &context);
card.value = *s;
s = strtok_s(NULL, ", ", &context);
card.suit = s;
card.drawn = false;
deck[index] = card;
index++;
}
相关文章:
- C++尝试深度复制唯一指针时出现内存访问冲突
- C++中的内存分配(引发异常:读取访问冲突)
- 在尝试使用CUDA分配内存时,我遇到了访问冲突写入位置错误
- 使用加速进程间创建消息队列 - 内存访问冲突
- C 指针转换会导致内存访问冲突
- 为什么创建进程 API 调用会导致内存访问冲突?
- 访问共享内存时出现读取访问冲突
- Python C++扩展 - 内存泄漏或访问冲突
- 如何修复<程序名称>中的"<内存位置>未处理的异常。Visual Studio 2017 中的访问冲突写入位置<内存位置>"
- 使用 std::map 作为本地成员的内存访问冲突
- 编译哈希算法时出现内存访问冲突错误
- 使用内存访问冲突错误
- 堆栈引发内存访问冲突
- 使用 Boost::序列化时内存访问冲突
- 调用std::vector::empty()时内存访问冲突读取位置
- 简单加密程序集程序-写入内存位置时发生访问冲突
- 动态分配内存时发生访问冲突
- 基于 SAPI 的应用程序在枚举令牌时引发内存访问冲突
- 内存访问冲突
- 访问冲突写入位置0x00000000.内存集函数问题