0

It's been a full semester since I've taken Data Structures so I decided to work on a project to refresh my memory.

I am building a dice game where the user defines the number of players. There's a lot of functions missing but I've included those that the program needs to run smoothly.

Proper destruction was occurring before I implemented and use Player type functions. Now, when *temp tries to leave scope of Player storeName(Player *p) , a break point is triggered at ~Player() { delete[] player; } and I can't understand why.

Note* The functions that I am not including are also type Player.

Here's an image for reference [1]: https://i.stack.imgur.com/Ib9WB.jpg

Any advice is appreciated.

struct Player {
    int* qualifiers;
    int matchPoints;
    int totalWins; 
    int numOfRolls; 
    string name; 
    
    Player() { qualifiers = new int[2]; }

    ~Player() { delete[] qualifiers; }
};

class Midnight {
public:
    Midnight(int numOfPlayers) {
    index = 0;
    m_numOfPlayers = numOfPlayers;
    player = new Player[m_numOfPlayers];

    for (int i = 0; i < m_numOfPlayers; i++) {
        player[i].matchPoints = 0;
        player[i].totalWins = 0;
        player[i].numOfRolls = 0;
        for (int j = 0; j < 2; j++) {
            player[i].qualifiers[j] = 0;
        }
    }

    dice = new int[player->numOfRolls];
    for (int i = 0; i < player->numOfRolls; i++) {
        dice[i] = 0; 
    }
}

    void welcome(); 

    Player storeName(Player* p);

    Midnight(const Midnight& source);

    Midnight operator=(const Midnight& source) {

        delete[] player; 

        if (&source == this) {
            return *this;
        }
        index = source.index;
        m_numOfPlayers = source.m_numOfPlayers;
        player = new Player[m_numOfPlayers]; 

        for (int i = 0; i < m_numOfPlayers; i++) {
            player[i].matchPoints = source.player[i].matchPoints;
            player[i].totalWins = source.player[i].totalWins;
            player[i].numOfRolls = source.player[i].numOfRolls;
            player[i].name = source.player[i].name;
            for (int j = 0; j < 2; j++) {
                player[i].qualifiers[j] = source.player[i].qualifiers[j];
            }
        }

        for (int i = 0; i < player->numOfRolls; i++) {
            dice[i] = source.dice[i];
        }
        return *this;
    }

    ~Midnight() {
        delete[] player; 
    }

private:
    int m_numOfPlayers;
    Player* player; 
    int *dice; 
    int index;
};

void Midnight::welcome() {
    // say stuff
    storeName(player);
}

// Store Name

Player Midnight::storeName(Player* p){
    Player* temp = p;
    
    // do stuff

    return *temp;
}

int main() {

    int players = 0;
    cout << "Enter a number of players: " << endl; 
    cin >> players; 

    Midnight Play(players);
    Play.welcome();
}
dweller
  • 9
  • 3
  • Questions seeking debugging help should generally provide a [mre] of the problem, which includes all `#include` directives, as well as the exact input required to reproduce the problem. – Andreas Wenzel Jul 24 '22 at 00:15
  • 1
    This bug is called "Pointless Use Of Pointers". Nothing in the shown code requires pointers, `new`, and `delete`. Getting rid of all of that, and replacing that with vectors completely eliminates `operator=`, including its own bug which you haven't hit yet, as well as the problematic code that results in your memory corruption (Rule of 3 violation). Just say "No" to pointless use of pointers! The right fix here is to get rid of all pointers, `new`s, and `delete`s. – Sam Varshavchik Jul 24 '22 at 00:17
  • You also failed to initialize all of the member variables of `Player` in the default constructor. – PaulMcKenzie Jul 24 '22 at 01:08
  • [Here is a cleaned up version of what you're trying to do](https://godbolt.org/z/YTnbbdsP4). No new, no delete, no assignment operator, etc. – PaulMcKenzie Jul 24 '22 at 01:21
  • @PaulMcKenzie man, that is so clean. We weren’t taught to use vectors in my data structures class. All the projects we wrote involved dynamically allocated memory. Thank you, so much. – dweller Jul 24 '22 at 04:04

0 Answers0