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?
Re: Array of pointers issue
Quote:
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.
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.
Re: Array of pointers issue
Thanks everyone! jnmacd's solution fixed it up. Also, what are code tags?
Re: Array of pointers issue
[.code][./code]
(without the periods)
It keeps formatting so indents are preserved.
Re: Array of pointers issue
Quote:
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.
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?
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.
Re: Array of pointers issue
Yes, however, doing it that way requires you to manage the eventual deletion of all pieces as well.
Re: Array of pointers issue
You could put the pointers in a vector with a custom allocator to destroy them for you.