简体   繁体   English

在纸牌游戏中检测到堆栈粉碎 C++

[英]Stack Smashing Detected C++ in Card game

So im programming a poker game (because im bored) and just setting out the classes and testing it works as i go along and its working perfectly BUT suddenly i add some new code to have an actual deck instead of infinite random cards and i just get this error所以我正在编写一个扑克游戏(因为我很无聊)并且只是设置类并测试它在我前进的过程中工作并且它完美地工作但是突然我添加了一些新代码来拥有一个实际的甲板而不是无限的随机卡片,我只是得到这个错误

*** stack smashing detected ***: <unknown> terminated
Aborted (core dumped)

This is from a g++ compiler on mint 19.3 cinnamon这是来自 mint 19.3 cinnamon 上的 g++ 编译器

I looked at other questions that are similar to mine but they seem to be about large amounts of data and i dont really see how thats in my program.我查看了与我的类似的其他问题,但它们似乎与大量数据有关,我真的不知道在我的程序中是怎样的。

If someone could help out or at least explain the error message that would be great如果有人可以提供帮助或至少解释错误消息那会很棒

-thanks -谢谢

/ my code / / 我的代码 /

#include <iostream>
#include <stdlib.h>

using namespace std;


class Card{
public:
    static Card* deck;
    static int current;
    char house;
    char value;

    void setTo(Card c){
        house = c.house;
        value = c.value;
    }

    void random(){
        setTo(*(deck + current));
        current++;
    }

    void print(){

        switch (value){
            case 11:
                cout << "jack";
                break;
            case 12:
                cout << "queen";
                break;
            case 13:
                cout << "king";
                break;
            case 14:
                cout << "ace";
                break;
            default:
                cout << (int)value;
                break;
        }

        cout << " of ";
        switch (house){
            case 0:
                cout << "spades";
                break;
            case 1:
                cout << "clubs";
                break;
            case 2:
                cout << "hearts";
                break;
            case 3:
                cout << "diamonds";
                break;
            default:
                cout << "there has been an error, the house is invalid";
                break;
        }
        cout << endl;
    }

    static void CreateDeck(){
        Card cs[52];
        deck = &cs[0];

        int k;
        for(int i = 0;i<4;i++){
            for(int j = 0;j<14;j++){
                k =  (i*13) + j;
                deck[k].house = i;
                deck[k].value = (j+1);
            }
        }
    }

    static void ShuffleDeck()
        int j,k;
        Card t;
        for(int i = 0;i<52;i++){
            j = rand() % 52;
            k = rand() % 52;
            t.setTo(*(deck+j));
            (*(deck+j)).setTo(*(deck+k));
            (*(deck+k)).setTo(t);
        }
    }
};

class Player{
    public:
        int chips;
        Card* hand;
        string pName;

        void initialize(string n){
            chips = 1000;
            pName = n;
            Card cs[2];
            hand = &cs[0];
        }

        void print(){
            cout << "player: " << pName << endl;
            cout << "    ";
            (*hand).print();
            cout << "    ";
            (*(hand +1)).print();
            cout << "    " << chips << " chips" << endl;
            cout << endl;
        }

        void deal(){
            (*hand).random();
            (*(hand+1)).random();
        }

};

class Game{
    public:
        int pot;
        Card* deck;

        void initialize(){
            pot = 0;
            Card c[5];
            deck = &c[0];
        }
};

Card* Card::deck = NULL;
int Card::current = 0;

int main()
{

    srand (time(NULL));
    Card::CreateDeck();
    Card::ShuffleDeck();
    Card b[2];
    b[0].random();
    b[1].random();

    b[0].print();
    b[1].print();
    cout << endl;
    return 0;
}

Your problem is here in createDeck :您的问题在createDeck

    Card cs[52];
    deck = &cs[0];

You have deck point to a local variable in the function.您有deck指向函数中的局部变量。 When the function exits the variable goes out of scope, so attempting to dereference deck invokes undefined behavior .当函数退出时,变量超出范围,因此尝试取消引用deck调用未定义的行为

The simplest fix is to dynamically allocate an array using new :最简单的解决方法是使用new动态分配数组:

deck = new Card[52];

And have a cleanup routine to delete [] the memory.并有一个清理例程来delete []内存。

A better way would be to define it as a std::vector :更好的方法是将其定义为std::vector

class Card{
public:
    static std::vector<Card> deck;

...

std::vector<Card> Card::deck(52);

This gives you better control over the memory.这使您可以更好地控制内存。 You will however need to change any explicit pointer arithmetic and derefernce to array subscript notation (ie *(deck + x) --> deck[x] since std::vector doesn't support these operators.然而,您将需要更改任何显式指针算术并取消对数组下标符号的引用(即*(deck + x) --> deck[x]因为std::vector不支持这些运算符。

Also in createDesk , you're going off the end of the array/vector here:同样在createDesk ,您将在这里结束数组/向量:

for(int j = 0;j<14;j++){

You want one less:你要少一个:

for(int j = 0;j<13;j++){

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

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