Thread: Passing pointer into function bug

  1. #1
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937

    Passing pointer into function bug

    Pointer problem: if that's all you want to know, skip next paragraph and go straight to code.

    As part of a tic-tac-toe system I am writing, I have a "board" object. It holds a 2d array of "cell" objects. The problem member function of "board" is "move()", which modifies to board. I also have an argument that is a pointer to a board. If this is not NULL, I want the state of the would-be changed board copied into the board the pointer points to. I'm having trouble with "if(not NULL)" part of the "move()" function. So, I have included the function and "board" 's overloaded operator =.
    Oh, and I might want to add that "access()" is a private function that returns a reference to the corresponding cell in the board.

    Code:
    RETURN_NOTICE board::move(short int which_player, int x_in, int y_in, board* out = NULL)
    {
          board * temp;
          if(x_in > 2 || y_in > 2 || x_in < 0 || y_in < 0 || which_player < 1 || which_player > 2) return OUT_OF_BOUNDS;
          if(get_cell(x_in,y_in).is_occupied()) return NO_VACANCY;
          if(out == NULL)
          {
           access(x_in, y_in)->assign((which_player == 1) ? P1_symbol : P2_symbol);
          }
          else
          {
           *temp = *this;
           temp->move(which_player, x_in, y_in, NULL);
           *out = *temp;  // DEBUG -- ILLEGAL OPERATION HERE
          }
          return SUCCESS;
    }
    Code:
    board board::operator = (board other)
    {
          cout << "P1_symbol = other.get_P1();" << endl;//DEBUG
          P1_symbol = other.get_P1();
          cout << "P2_symbol = other.get_P2();" << endl;//DEBUG
          P2_symbol = other.get_P2();
          for(int a = 0; a < 3; a++)
          {
                  for(int b = 0; b < 3; b++)
                  {
                          cout << "*access(a,b) = other.get_cell(a,b);" << endl;//DEBUG
                          *access(a,b) = other.get_cell(a,b);
                  }
          }
          cout << "return *this;" << endl; //DEBUG
          return *this;
    }
    and... for good measure... the object itself.
    Code:
    class board
    {
          cell cells[3][3];
          char P1_symbol;
          char P2_symbol;
          inline cell * access(int x_in, int y_in);//for class to read/write itself in reg (x,y) plane style 
          public:
                 board();
                 board(char P1_in, char P2_in);
                 ~board();
                 cell get_cell(int x_in, int y_in);
                 inline char get_P1() {return P1_symbol;}
                 inline char get_P2() {return P2_symbol;}
                 board operator = (board other);
                 RETURN_NOTICE move(short int which_player, int x_in, int y_in, board* out);
    };
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    28
    Code:
    *temp = *this;
    What's going on here? It looks like temp is an uninitialised pointer to a board, so it's pointing off into space for all we know, and then you dereference it and try to create a board at the memory address it's pointing to. Try allocating some space where the pointer is pointing.

    Code:
    temp = new board;
    OTOH, it's early morning here and I'm half asleep, so take this for what it's worth.
    "C++ is like jamming a helicopter inside a Miata and expecting some sort of improvement."
    - Drew Olbrich

  3. #3
    Registered User
    Join Date
    Sep 2001
    Location
    Fiji
    Posts
    212
    First of all use if () else if () else structure in your board::move() method. Makes no difference its just for clarity and a good practice for this situation.

    Second as Just said, don't dereference "this" pointer. There is no need to do that.

    See if that resolves the issue first. Else if it doesn't post more code.

  4. #4
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    yup. Thanks. Now, any reasons why this code might execute extremely slowly?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    28
    Quote Originally Posted by kwigibo
    Second as Just said, don't dereference "this" pointer. There is no need to do that.
    I wasn't complaining about dereferencing the this pointer, I was complaining about dereferencing the variable temp. Was that the problem, CodeMonkey?

    Quote Originally Posted by CodeMonkey
    Now, any reasons why this code might execute extremely slowly?
    Not from what you've posted - we'd probably need to see more code.
    "C++ is like jamming a helicopter inside a Miata and expecting some sort of improvement."
    - Drew Olbrich

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Replies: 9
    Last Post: 12-25-2007, 05:01 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Passing a function pointer to a templated type
    By skorman00 in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2004, 08:31 PM