简体   繁体   English

堆栈抛出内存访问冲突

[英]Stack throws memory access violation

So I am trying to create a simple poker game to re familiarize myself with C++, as I have been out of it for a while and would like to get back into C++. 因此,我正在尝试创建一个简单的扑克游戏,以使自己重新熟悉C ++,因为我已经离开了一段时间,并且想重新使用C ++。

I am having issues with a stack (appropriate, no?), from the STL. 我在STL中遇到堆栈问题(适当的,不是吗?)。 I can't seem to get it to allow me to add anything to the stack. 我似乎无法获得允许我向堆栈中添加任何内容的功能。 Every time I call the push function on the stack object, it throws a memory access violation... I even tried creating a stack of integers, and adding those to the stack, but it still throws the memory access violation error. 每次我在堆栈对象上调用push函数时,都会引发内存访问冲突……我什至尝试创建一个整数堆栈,并将这些整数添加到堆栈中,但是仍然会引发内存访问冲突错误。 Which is weird. 这很奇怪。

The code that currently adds the object to the stack is: 当前将对象添加到堆栈的代码是:

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

I set a break point, and followed it down the rabbit hole, and found out it throws the exception here: 我设置了一个断点,然后将其跟踪到兔子洞中,发现它在此处引发了异常:

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

Which is located deque.cpp... Which is odd. 哪个位于deque.cpp ...这很奇怪。

For reference, here is the rest of the code I think may be necessary: 作为参考,以下是我认为可能需要的其余代码:

Deck.cpp Deck.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 (contains the CARD struct) 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();

        };
    }

You're probably writing past the end of the deck array in loadCards() , corrupting memory. 您可能正在通过loadCards()中的deck数组的末尾写loadCards() ,从而破坏了内存。

Even if you have exactly 52 lines of data, cardlist.good() will return true until after an EOF indication. 即使你有数据的确切52行, cardlist.good()将返回true ,直到 EOF指示。

You might want to try the following loop instead (untested): 您可能想尝试以下循环(未经测试):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM