-
October 21st, 2010, 09:48 PM
#1
Array of pointers issue
I'm attempting to make a game of 3D chess, in which there are piece, board, and coordinates classes. In the board class, there is an [8][8][8] array of pointers to pieces. In the piece's constructor, I have the relevant pointer in the board point to the piece, but it doesn't seem to work correctly. When I try to print the name, it gives me a strange character, which when cast to int is -52, instead of 'Q'. Here is the relevant code:
Main.cpp:
board* homeBoard = 0;
int main() {
int i;
setup();
piece q ('Q', 1, coordinates(5,4,2));
piece* pq = homeBoard->lookup(5,4,2);
cout << pq->name;
cin >> i;
}
void setup() {
delete homeBoard;
homeBoard = new board();
piece::setHomeBoard(homeBoard);
}
Piece.cpp:
void piece::setHomeBoard(board* pb) {
homeBoard = pb;
}
piece:iece(char id, bool team, coordinates c) {
this->name = id;
//otherdata = 0; //Stored: **** T M C M(m)
//otherdata |= (team&1)<<3;
//otherdata |= findMultiMove()&1;
//loadMoveTypes();
//location = c;
//whereMove = whereCapt = whereCast = 0;
//findMoves(true);
homeBoard->place(*this,c);
}
Board.cpp:
piece* board::lookup(coordinates c) const {
return lookup(c.getX(), c.getY(), c.getZ());
}
piece* board::lookup(int x, int y, int z) const {
if(0<=x && x<=7 && 0<=y && y<=7 && 0<=z && z<=7) return table[x][y][z];
return 0;
}
void board:lace(piece p, int x, int y, int z) {
table[x][y][z] = &p;
}
Does anybody know what went wrong? Also, the coordinates class is working perfectly, no matter what I throw at it, so I don't think it's that. And the header files work too.
-
October 22nd, 2010, 07:17 AM
#2
Re: Array of pointers issue
Looks like overuse of pointers to me; you often don't need them at all. I don't think I have enough information to comment on what might be wrong though. And please wrap your code in code tags to preserve formatting.
I do find it suspicious that "homeBoard" appears to be both a global and a static member of piece. Is it possible you have two variables with the same name?
-
October 22nd, 2010, 08:29 AM
#3
Re: Array of pointers issue
Originally Posted by Lindley
I do find it suspicious that "homeBoard" appears to be both a global and a static member of piece. Is it possible you have two variables with the same name?
I think it's possible because they are located in different scopes. But it's a dangerous practice that provokes confusion.
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
October 22nd, 2010, 10:41 AM
#4
Re: Array of pointers issue
Code:
void board:place(piece p, int x, int y, int z) {
table[x][y][z] = &p;
}
So you are passing in a local copy of piece... then storing the memory address of a local variable.
THIS IS A BIG NO! As soon as the place() function ends, &p is no longer valid.
It should be instead something like,
Code:
homeBoard->place(this,c);
.......
void board:place(piece *p, int x, int y, int z) {
table[x][y][z] = p;
}
Go ahead and pass in the original object, not make a copy of it. The original object was created in main(), so it will still be valid through out the end of the program.
Last edited by jnmacd; October 22nd, 2010 at 10:45 AM.
-
October 22nd, 2010, 11:46 AM
#5
Re: Array of pointers issue
Thanks everyone! jnmacd's solution fixed it up. Also, what are code tags?
-
October 22nd, 2010, 11:52 AM
#6
Re: Array of pointers issue
[.code][./code]
(without the periods)
It keeps formatting so indents are preserved.
-
October 22nd, 2010, 11:56 AM
#7
Re: Array of pointers issue
Originally Posted by henryswanson
Thanks everyone! jnmacd's solution fixed it up. Also, what are code tags?
Do you understand why?
If you don't, you are going to run into a lot of problems as you make your code more complex.
-
October 22nd, 2010, 12:24 PM
#8
Re: Array of pointers issue
Yeah, it's because each of a function's parameters is a copy of what I pass to it, not the actual thing (unless it's passed by reference), so it's deleted when the function ends, right?
-
October 22nd, 2010, 01:51 PM
#9
Re: Array of pointers issue
Yes, so basically an object's lifetime.
I'm assuming your final product won't just create the pieces in main, but instead do something similar to your setup() function.
Understand that if you created this piece is your setup() function, i.e.
Code:
void setup() {
delete homeBoard;
homeBoard = new board();
piece::setHomeBoard(homeBoard);
piece q ('Q', 1, coordinates(5,4,2));
}
.......
int main() {
int i;
setup();
piece* pq = homeBoard->lookup(5,4,2);
cout << pq->name;
cin >> i;
}
You will now be at your same problem. You create a local object in setup(), that function ends, and destroys all locally created objects.
So your best bet since this needs to be persistent through the whole program is to create this object on the heap, i.e.
Code:
void setup() {
delete homeBoard;
homeBoard = new board();
piece::setHomeBoard(homeBoard);
piece *q = new piece ('Q', 1, coordinates(5,4,2));
}
Now it will not be destroyed when setup() ends.
-
October 22nd, 2010, 02:00 PM
#10
Re: Array of pointers issue
Yes, however, doing it that way requires you to manage the eventual deletion of all pieces as well.
-
October 22nd, 2010, 02:08 PM
#11
Re: Array of pointers issue
You could put the pointers in a vector with a custom allocator to destroy them for you.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|