Chess game

I am trying to make a chess game, however I am having issues drawing the array I populate onto the screen. I receive what appears to be a memory error:


C++(2919) malloc: *** mmap(size=3420891154821050368) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
libc++abi.dylib: terminate called throwing an exception


The following is the code I am using:

The Board object
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include "Piece.h"

class Board
{
public:
    void setBoard(int p1, int p2, Piece t);
    void draw();
    Board();
    ~Board();
private:
    Piece board[8][8];
};

void Board::setBoard(int p1, int p2, Piece t)
{
    board[p1][p2] = t;
}

void Board::draw()
{
    std::cout<<"  ";
    for (int i = 0; i< 8; i++)
        std::cout<<i+1<<" |";
    
    int k = 0;
    int j = 0;
    std::cout<<"\n"<<j+1<<"|";
    for (int i = 0; i < 64; i++)
    {
        std::cout<<board[j][k].getType()<<board[j][k].getName()<<"|";
        k++;
        if ( k == 8)
        {
            j++;
            std::cout<<"\n"<< j+1<<" |";
        }
    }
}

Board::Board()
{
    
}

Board::~Board()
{
    
}


The Piece object

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Piece
{
public:
    std::string getName();
    void setType(char t);
    char getType();
    Piece();
    ~Piece();
    
protected:
    std::string name;
    char type;
    int p1,p2;
};

std::string Piece::getName()
{
    return name;
}

void Piece::setType(char t)
{
    type = t;
}
char Piece::getType()
{
    return type;
}

Piece::Piece()
{
    name = " ";
    type = NULL;
}
Piece::~Piece()
{
    
}


This is the Pawn object, all the other objects are the same except for name change

1
2
3
4
5
6
7
8
9
10
11
12
class Pawn : public Piece
{
public:
    Pawn(char t);
private:
};

Pawn::Pawn(char t)
{
    name = "P";
    type = t;
}


As you can see I am inheriting from the Piece object.

Here is the function that populates the board

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
void Initialise(Board &table)
{
    //Pawns
    for (int i = 0; i < 8; i++)
        table.setBoard(1, i, *new Pawn('B'));
    for (int i = 0; i < 8; i++)
        table.setBoard(6, i, *new Pawn('W'));
    //Rooks
    table.setBoard(0, 0, *new Rook('B'));
    table.setBoard(0, 7, *new Rook('B'));
    table.setBoard(7, 0, *new Rook('W'));
    table.setBoard(7, 7, *new Rook('W'));
    //Knights
    table.setBoard(0, 1, *new Knight('B'));
    table.setBoard(0, 6, *new Knight('B'));
    table.setBoard(7, 1, *new Knight('W'));
    table.setBoard(7, 6, *new Knight('W'));
    //Bishops
    table.setBoard(0, 2, *new Bishop('B'));
    table.setBoard(0, 5, *new Bishop('B'));
    table.setBoard(7, 2, *new Bishop('W'));
    table.setBoard(7, 5, *new Bishop('W'));
    //Queens
    table.setBoard(0, 3, *new Queen('B'));
    table.setBoard(7, 4, *new Queen('W'));
    //Kings
    table.setBoard(0, 4, *new King('B'));
    table.setBoard(7, 3, *new King('W'));
    //empty peices
    int j = 2;
    int k = 0;
    for (int i = 0; i < 32; i++)
    {
        table.setBoard(j, k, *new Piece);
        k++;
        if ( k == 8)
        {
            j++;
            k = 0;
        }
    }
}


As far as data output goes

1 |2 |3 |4 |5 |6 |7 |8 |
1|BR|BKn|BB|BQ|BKi|BB|BKn|BR|


After this I get the error message which makes me think it is to do with the spaces that are not supposed to have a a playable piece. After the error message the following is also output.


2 |BKi|BKi|BKi|BKi|BKi|BKi|BKi|BKi|BKi|BKi|BKi|BKi|BKi|BKi|BKi|BKi|BKi|BKi|BKi|BKi|BKi|BKi|BKi|BKi|BKi|BKi|BKi|BKi|BKi|BKi|BKi|BKi|WP|WP|WP|WP|WP|WP|WP|WP|WR|WKn|WB|WKi|WQ|WB|WKn|WR|\341\370\277_\377\210\370\277_\37788`\356\277_\377p\370\277_\377G
8\331\303_\377\321\303_\377||4\372\277_\377\272\372\277_\377\363\372\277_\377::`\356\277_\377p\370\277|\225\373\277_\377\334\373\277_\377\374\277_\377;;`\356\277_\377|m\374\277_\377\341\374\277_\377]\375\277_\377<<`\356\277_\377p\370\277_\377G
8\331\303_\377\321\303_\377H\322\303_\377\350\322\303_\377\210\323\303_\377|\376\277_\377\230\376\277_\377|\377\277_\377\253\377\277_\377\312\377\277_\377>>`\356\277_\377p\370\277_\377G
8\331\303_\377\321\303_\377|r


Any help would be much appreciated.
Last edited on
You've got a memory leak right now. You create the array of your pieces as static objects, which cannot be assigned the address of an object created on the free store. when the function returns, the pointer is lost.

Consider either creating an array of piece pointers, or just creating temporary objects which you send to setBoard.

1
2
3
4
5
6
7
//Rooks
    table.setBoard(0, 0, Rook('B'));
    table.setBoard(0, 7, Rook('B'));
    table.setBoard(7, 0, Rook('W'));
    table.setBoard(7, 7, Rook('W'));

//etc... 


You should also overload the assignment operator.
Last edited on
Topic archived. No new replies allowed.