Search code examples
c++classrecursiondatamemberstack-frame

When passing an data member array into a recursive procedure, is a new copy of that array created in each stack frame?


I am facing the issue that copy of an 2 dimensional array is not made in each stack frame of recursive call. I am doing indirect recursion.

I also tried sending my data in function call from main() function but the copy was not made. Same address was used in every recursive call.

class board
{


public:

    int board_arr[8][8];

public:
    board()
    {

    }

    void player1Turn()
    {

        for (int i = 0; i < rowCount; i++)
        {
            for(int j = 0; j < rowCount; j ++ )
            {
                if (board_arr[i][j] == 1)
                {
                    //checking if the pawn can move anywhere
                    if (i + 1 <=7 && j - 1 >= 0 && board_arr[i + 1][j - 1] == 0 )
                    {
                        board_arr[i][j] = 0;
                        board_arr[i + 1][j - 1] = 1;

                        player2Turn();

                    }
                    if (i + 1 <=7 && j + 1 <= 7 && board_arr[i + 1][j + 1] == 0)
                    {
                        board_arr[i][j] = 0;
                        board_arr[i + 1][j + 1] = 1;


                        player2Turn();

                    }
                    //opponent infront 
                    //killing
                    //if opponent is infront and checking if you can kill it or not
                    if (i + 2 <= 7
                        && i + 1 <= 7 
                        && j - 2 >=0 

                        && j - 1 >= 0 
                        && board_arr[i + 1][j - 1] == 2
                        && (board_arr[i + 2][j - 2]==0)) 
                    {
                            board_arr[i][j] = 0;
                            board_arr[i + 2][j - 2] = 1;
                            board_arr[i + 1][j - 1] = 0;

                            cout << endl << "kill by p1 " << endl;


                            player2Turn();

                    }
                    if (i + 2 <= 7 
                        && i + 1 <= 7 
                        && j + 2 <= 7

                        && j + 1 <=7 
                        && board_arr[i + 1][j + 1] == 2 
                        && (board_arr[i + 2][j + 2]==0))
                    {
                        board_arr[i][j] = 0;
                        board_arr[i + 1][j + 1] = 0;
                        board_arr[i + 2][j + 2] = 1;


                        cout << endl << "kill by p1 " << endl;

                        player2Turn();

                    }
                }

            }

        }

    }
    void player2Turn()
    {

        for (int i = rowCount-1; i >= 0; i--)
        {
            for (int j = rowCount-1; j >= 0; j--)
            {
                if (board_arr[i][j] == 2)
                {
                    //checking if the pawn can move anywhere
                    if (i - 1 >= 0 && j - 1 >= 0 && board_arr[i - 1][j - 1] == 0)
                    {
                        board_arr[i][j] = 0;
                        board_arr[i - 1][j - 1] = 2;


                        player1Turn();

                    }
                    if (i - 1 >= 0 && j + 1 <=7 && board_arr[i - 1][j + 1] == 0)
                    {
                        board_arr[i][j] = 0;
                        board_arr[i - 1][j + 1] = 2;


                        player1Turn();

                    }
                    //opponent infront 
                    //killing
                    //if opponent is infront and checking if you can kill it or not
                     if (i - 2 >= 0
                        && i - 1 >= 0
                        && j - 2 >= 0

                        && j - 1 >= 0
                        && board_arr[i - 1][j - 1] == 1
                        && (board_arr[i - 2][j - 2] ==0))
                    {
                        board_arr[i][j] = 0;
                        board_arr[i - 2][j - 2] = 2;
                        board_arr[i - 1][j - 1] = 0;

                        cout << endl << "kill by p2 " << endl;

                        player1Turn();



                    }
                    if (i + 2 <= 7
                        && i - 1 >= 0
                        && j + 2 <=7

                        && j + 1 <= 7
                        && board_arr[i - 1][j + 1] == 1
                        && (board_arr[i - 2][j + 2] ==0))
                    {
                        board_arr[i][j] = 0;
                        board_arr[i - 2][j + 2] = 1;
                        board_arr[i - 1][j + 1] = 0;

                        cout << endl << "kill by p1 " << endl;

                        player1Turn();

                    }
                }

            }

        }
    }

};

same copy of the board_arr was used in each call.


Solution

  • You are not passing board_arr to a recursive method, that is those methods do not have that array in their parameters. So board_arr is not being copied.

    Because those methods are instance methods of board class, everything is passed in each method call is this pointer to the instance of board.