Array of pointers issue
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: Array of pointers issue

Hybrid View

  1. #1
    Join Date
    Oct 2010
    Location
    St Paul, MN
    Posts
    60

    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.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,885

    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?

  3. #3
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,571

    Re: Array of pointers issue

    Quote Originally Posted by Lindley View Post
    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.

  4. #4
    Join Date
    Jun 2009
    Location
    oklahoma
    Posts
    199

    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.

  5. #5
    Join Date
    Oct 2010
    Location
    St Paul, MN
    Posts
    60

    Re: Array of pointers issue

    Thanks everyone! jnmacd's solution fixed it up. Also, what are code tags?

  6. #6
    Join Date
    Jun 2009
    Location
    oklahoma
    Posts
    199

    Re: Array of pointers issue

    Quote Originally Posted by henryswanson View Post
    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.

  7. #7
    Join Date
    Oct 2010
    Location
    St Paul, MN
    Posts
    60

    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?

  8. #8
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Array of pointers issue

    [.code][./code]

    (without the periods)

    It keeps formatting so indents are preserved.

  9. #9
    Join Date
    Jun 2009
    Location
    oklahoma
    Posts
    199

    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.

  10. #10
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,885

    Re: Array of pointers issue

    Yes, however, doing it that way requires you to manage the eventual deletion of all pieces as well.

  11. #11
    Join Date
    Jan 2009
    Posts
    1,689

    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center