Allocating Memory To A Pointer Of An Object Created From A Class Of Array

I am so new in C++ programming.
I am trying to write a code for a chess engine..

here is my class declaration:
//for the board class
struct boardTemp
{ private:
sqrTemp boardSqr[64]; //[63] was inaccurate representation.
int blkPoints, whtPoint;
string role; //white or black
string turn; //while or black
int epSqr; //en passant square
clock_t wtime, btime; //time left to the next control
int wMoveremaining, bMoveremaining;
bool bCastleQ, bCastleK, wCastleQ, wCastleK;

public:
void boardSetPiece(int boardSqrNum, string boardPiece)
{boardSqr[boardSqrNum-1].sqrSetPiece(boardPiece); }
void boardSetColor(int boardSqrNum, string pieceColorTmp)
{boardSqr[boardSqrNum-1].sqrSetColor(pieceColorTmp); }
void boardSetSqrNum(int boardSqrNum)
{boardSqr[boardSqrNum-1].sqrSetSqrNum(boardSqrNum); }
string boardGetTurn()
{return turn;}
string boardGetRole()
{return role;}
int boardGetEPSqr()
{return epSqr;}
string boardGetPiece(int boardSqrNum)
{return boardSqr[boardSqrNum-1].getPiece(); }
string boardGetColor(int boardSqrNum)
{return boardSqr[boardSqrNum-1].getColor(); }
int boardGetSqrNum(int boardSqrNum)
{return boardSqr[boardSqrNum-1].getSqrNum(); }
}
// for the square class
struct sqrTemp
{
private:

string pieceSqr; // pawn, rook, knight, bishop, queen, king, null
string pieceColorSqr; // black, white, null
int sqrNum;
public:

void sqrSetPiece(string a)
{pieceSqr = a;}
void sqrSetColor(string b)
{pieceColorSqr=b;}
void sqrSetSqrNum(int c)
{sqrNum=c;}
string getPiece(){
return pieceSqr;}
string getColor(){
return pieceColorSqr;}
int getSqrNum() {
return sqrNum;}

};

my main file contains this:

boardTemp * mainBoard;
mainBoard = new boardTemp;
int i=1,j=8; // j=rank counter, i= file counter
string pieceMB, colorMB; // piece and the color of the piece.

do{i=1; //loops from rank 8 file1 to file 8 then moves to rank 7 file 1 - to file 8
do{
cout <<"Enter the Piece in square "<< flrnkSqr(i,j)<<endl;
cin >> pieceMB;cout <<endl;
mainBoard->boardSetPiece(flrnkSqr(i,j),pieceMB);
cout << &mainBoard->boardGetPiece(flrnkSqr(i,j));
cout <<"Enter the Piece Color in square "<< flrnkSqr(i,j)<<endl;
mainBoard->boardSetColor(flrnkSqr(i,j),colorMB);
mainBoard->boardSetSqrNum(flrnkSqr(i,j));

i+=1;
} while (i<=8); j-=1;
} while (j>=1);

----
but after i reaches 8, it gives a message saying unhandled exception....access violation reading allocation...
By the way, the classes are declared in a header file.
Thanks.
Last edited on
please use code tags!!.. your code is very hard to read this way..
Start by not using pointers unnecessarily. To create an object of type boardTemp, write boardTemp mainBoard;

Then write some constructors: chess board squares whose color and contents are unset do not exist in real world, why do they exist in your program?

The board itself has a well-known default configuration, so the default constructor of the boardTemp class should already populate, or at least colorize every square.

As for tracking down the error you're seeing, use the debugger, or post a compilable program that reproduces the error so that someone else would run it through a debugger for you.
Last edited on
Your loops are probably going past the end of the array.

I would also recommend not having multiple statements on 1 line.
Last edited on
when you create an array the index extends from 0 to n, where n is 1- the amount you requested. So when you have your loop go through to the maximum increment it rEads past the end of the array. Try setting the while statement so that it loop until i < 8.
Thanks for your responses:

@ Cubbi >> the colour does not pertain to the colour of the square, it pertains to the colour of the piece in the square. In abstract chess, squares don't really need to be coloured but the pieces should be. Not really colour but at least something to discriminate the piece, from you own or opponent.

@ pogrady and TheIdeasMan >> there are 64 squares on a chess board, created a board class with an array of 64 (0 -63) sqrTemp boardSqr[63]; this mean that square 1 would be [0] in the array. but I guess you are right, when I changed " sqrTemp boardSqr[63]" to "sqrTemp boardSqr[64];" I was able to provide what piece and what colour. It is just that, it has 1 extra square that what it represents in real world. By the way, this is in preparation to to accommodate FEN notation (if you have heard of it). I am just making sure that it would accept data transfer in that manner.

@ Jikax >> sorry, I am very new to C++ programming or programming for that matter and I am not aware yet of the conventions that are used by professional coders. I just started the code after I watched some videos on youtube about C++ programming.


while (i<=8);

I meant that the less than equal operator causes the overflow. Realise that the loop ends when the condition becomes false. So making the array bigger is not really a solution.

IF you post your code with code tags as I and others have said, you might get more replies & advice.

HTH
@ TheIdeasMan >>That would not work, It would not loop to file 8. I labelled the squares on the board in this manner:
rank 8 |57 58 59 60 61 62 63 64
rank 7 |49 50 51 52 53 54 55 56
rank 6 |41 42 43 44 45 46 47 48
rank 5 |33 34 35 36 37 38 39 40
rank 4 |25 26 27 28 29 30 31 32
rank 3 |17 18 19 20 21 22 23 24
rank 2 |09 10 11 12 13 14 15 16
rank 1 |01 02 03 04 05 06 07 08
--------------------------------------

the entry of the pieces starts from rank 8 going to the right then moves down.
but after i reaches 8, it gives a message saying unhandled exception....access violation reading allocation...


These sort of errors are nearly always due to going past the end of the array.

What if you change to :

while (i<8);

You still have no code tags - so it is hard to read your code properly For that reason I might be wrong.
jrfrago wrote:
there are 64 squares on a chess board, created a board class with an array of 64 (0 -63) sqrTemp boardSqr[63]; this mean that square 1 would be [0] in the array.


sqrTemp boardSqr[63]; does not create an array with 64 elements, addressable by indices 0-63. It creates an array of 63 elements addressable by indices 0-62.

jrfrago wrote:
but I guess you are right, when I changed " sqrTemp boardSqr[63]" to "sqrTemp boardSqr[64];" I was able to provide what piece and what colour. It is just that, it has 1 extra square that what it represents in real world


No. It would then have exactly the right number of elements, but as you didn't change it in the code above, I would suspect that is still your problem.
@cire >> that was my mistake. I was confused with the array[n], I thought it would have n+1 elements from 0 to n. Thanks. That explains why sqrTemp boardSqr[64] worked and why sqrTemp boardSqr[63] gives access violation reading allocation.


@TheIdeasMan>> cannot use 1<8 as it would miss the squares in file 8: 08, 16, 24, 32, 40, 48, 56, 64. I have tried moving the squares from left to right then up (instead of moving left to right then down) and it works till it reaches square 64. The array allocation explains why, I had a wrong understanding of the array element reservation.
Last edited on
Topic archived. No new replies allowed.