0

I'm building a domino game and trying to make a function that adds a Stone to a pile. This is the Stone class:

class Stone
{
public:
    Stone(){ left = 0; right = 0; };
    Stone(int x, int y){ left = x; right = y; };
    void turnStone();
    bool comStones(const Stone&);
    void printOpen();
    void printClosed();
    friend class Pile;
    friend class Game;
private:
    int left, right;
};

And this is the Pile class:

class Pile
{
public:
    Pile(){};
    Stone indexfun(int);
    void printOpen();
    void printClosed();
    void fillPile();
    void randPile();
    void addStone(Stone&);
    void remStone(Stone&);
    friend class Player;
    friend class Game;
private:
    Stone* list;
    int size=0;
};

Now the function I built for adding a stone to the pile is:

void Pile::addStone(Stone& newStone) {
    Stone* newList = new Stone[size + 1];
    int i;
    for (i = 0; i < size; i++)
    {
        newList[i] = list[i];
    }
    newList[i] = newStone;
    delete[] list;
    list = newList;
    size++;
}

When I try and build the newlist array it throws me out of the program. can't find the reason. Help anyone?

the previous functions and more classes in the project:

class Game
{
public:
    Game(char*);
    void run();
private:
    Player human, computer;
    Pile box, table;
    int start, finish;
    void playerMove(int);
    void checkPile(int);
    bool checkMove(int,int);
    bool whosFirst();
    bool checkGame();
    void printTable();
};
class Player
{
public:
    Player(){};
    void addStone(Stone&);
    void remStone(Stone&);
    void printPile();
    Stone* searchStone();
    friend class Game;
private:
    char* name;
    Pile pie;
};
void Game::run() {
    cout << "starting the game" << endl;
    box.fillPile();
    box.size = 28;
    box.randPile();
    for (int i = 0; i < 7; i++)
    {
        human.addStone(box.list[i]);
        box.remStone(box.list[i]);
        computer.addStone(box.list[i]);
        box.remStone(box.list[i]);
    }
}
void Player::addStone(Stone& added) {
    pie.addStone(added);
}

int main()
{
    char name[80];
    cout << "enter your name" << endl;
    cin >> name;
    Game game(name);
    game.run();
}
Adam Mo
  • 75
  • 7
  • 7
    Is there a reason you aren't using `std::vector`? – TartanLlama Mar 30 '15 at 08:42
  • In my course we use `using namespace std;` so that's why. – Adam Mo Mar 30 '15 at 08:46
  • "throws you out of the program"?? Hand come out of the computer window and chuck you somewhere? Or could you please elaborate. – therainmaker Mar 30 '15 at 08:48
  • 1
    @AdamMo That makes no sense, I think you have misunderstood something. [For one, `using namespace std;` is a bad idea.](http://stackoverflow.com/q/1452721/1171191) But it has nothing to do with whether or not you can use a [`vector`](http://en.cppreference.com/w/cpp/container/vector) – BoBTFish Mar 30 '15 at 08:48
  • Try initializing i to zero : `int i = 0;` – marcinj Mar 30 '15 at 08:48
  • And the reason the answer is "use a `std::vector`" is because handling memory yourself is easy to get wrong. Which you do, a lot. You are leaking memory like crazy in `addStone`. You need to `delete[]` the old `list`. – BoBTFish Mar 30 '15 at 08:49
  • I have no idea what `std::vector` is for, and the namespace method is the method we use in class so that is why i use it. also the i initializing has nothing to do with the problem. – Adam Mo Mar 30 '15 at 08:51
  • I can't see anything in this code that should crash you out, but you do have a memory leak. So your problem may be caused somewhere else in the code. To fix your memory leak initialize your `list` when you declare it using `Stone* list = nullptr;` and in your addStone()` function `delete[]` it with `delete[] list;` just before you assign over the new list with `list = newList;`. – Galik Mar 30 '15 at 08:59
  • @Galik I tried it now, still the dynamic allocation doesn't work :/ – Adam Mo Mar 30 '15 at 09:03
  • 1
    Are you calling any functions before `addStone()`? I have a simple loop adding 100000 stones and no crashing. – Galik Mar 30 '15 at 09:06
  • `std::vector` is basically a self-growing dynamic array. So if you add to it and it doesn't have enough space, it allocates new space internally without you worrying about it. You should definitely learn how to use `std::vector` *before* learning to write your own version, which is what you are basically doing. – BoBTFish Mar 30 '15 at 09:06
  • it goes to: void *__CRTDECL operator new[](size_t count) _THROW1(std::bad_alloc) { // try to allocate count bytes for an array return (operator new(count)); } – Adam Mo Mar 30 '15 at 09:06
  • Can you post your initialization and calling code? – Galik Mar 30 '15 at 09:08
  • I add some more code for you to look at at the main question – Adam Mo Mar 30 '15 at 13:59

1 Answers1

0

Maybe change the "list" variable name to "list1" or something like that. "list" is a STL container, just like vector.

Leśny Rumcajs
  • 2,259
  • 2
  • 17
  • 33