简体   繁体   中英

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

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 :

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

You have deck point to a local variable in the function. When the function exits the variable goes out of scope, so attempting to dereference deck invokes undefined behavior .

The simplest fix is to dynamically allocate an array using new :

deck = new Card[52];

And have a cleanup routine to delete [] the memory.

A better way would be to define it as a 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.

Also in createDesk , you're going off the end of the array/vector here:

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

You want one less:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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