PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, October 20, 2022

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

 October 20, 2022     c++, class, datamember, recursion, stack-frame     No comments   

Issue

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.



Answered By - Renat
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing