Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8713579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:22:31+00:00 2026-06-13T05:22:31+00:00

I am generating a large game tree that depicts an auction scenario between two

  • 0

I am generating a large game tree that depicts an auction scenario between two nodes (referred to as type1 and type2. The tree is generated perfectly fine till I reach the fourth level of nodes.

    #include <iostream>
    #include <vector>
    #include <queue>

    #define LEVEL 8

    using namespace std;

    class payoff_node{
        int agent_1;
        int agent_2;
    };

    class node{
        public:
            /* constructor to set payoff_ptr to NULL */
            node() { 
                payoff_ptr = NULL;
                parent = NULL; 
            }

            /* print the information about the node */
            void printNode(){
                cout << "type: " << type << "\t" << "level: " << level << "\t" << "purse: $" << purse << "\t" << "parent_bid: " << parent_bid << "\t" << "parent:" << parent->getType() << "\t" << endl;
                /*for (int i = 0; i < actions.size() ; i++){
                    cout << actions[i] << endl;
                }*/
            }

            void setType(int t){    type = t;   }

            void setLevel(int l){   level = l;  }

            void setPurse(int p){ 
                purse = p; 

                /* Depending on the purse value the action space is decided */
                for(int i = 0; i <= purse ; i++){
                    actions.push_back(new node);
                }
            }

            void setParentBid(int bid){ parent_bid = bid; }

            void setParent(node &parent_node){  parent = &parent_node;  }



            int getLevel(){ return level; }

            int getParentBid(){ return parent_bid;  }

            int getPurse(){ return purse; }

            node** getParent(){ return &parent; }

            int getType(){ return type; }


            vector<node*> actions;



        private:
            int type;
            int level;
            int purse;

            int parent_bid;
            node* parent;

            payoff_node* payoff_ptr;
    };

    int main(int argc, char* argv[]){
        bool D = false;
        /* Make the root node and set its properties */
        node root_node;
        root_node.setType(1);
        root_node.setLevel(1);
        root_node.setPurse(4);
        root_node.setParentBid(0);

    //  root_node.printNode();  
        queue<node> myQ;
        myQ.push(root_node);

        /* BFS like creation of perfect information tree */
        while(true){
            node &ext = myQ.front();
            if(D)
                cout << "h1" << endl;

            if(ext.getLevel() == LEVEL){
                break;
            }

            if(D)
                cout << "h2" << endl;

            int type = ext.getType();

            if(D)
            cout << "h3" << endl;
            for(int i = 0; i < ext.actions.size() ; i++){
                node &child = *(ext.actions[i]);
            if(D)
            cout << "h4" << endl;
                //process(child);

                /* set parent */
                child.setParent(ext);
            if(D)
            cout << "h5" << endl;

                /* set type  & items won so far */
                if((ext.getLevel()) % 2 == 0){    // i.e. even numbered level, then current round has ended 
                    if(i > ext.getParentBid()){
                        child.setType(type);    
                    }
                    else{
                        int type_val = ( *(*(ext.getParent()))).getType() ;
                        child.setType( type_val );
                    }
                }
                else{
                    if(type == 1){
                        child.setType(2);

                    }
                    else{
                        child.setType(1);

                    }
                }

            if(D)
            cout << "h6" << endl;
                /* set level */
                child.setLevel(ext.getLevel() + 1);
            if(D)
            cout << "h7" << endl;
                /* set purse */
                if(child.getType() == ext.getType()){
                    int val = ext.getPurse() - i;
                    if(val < 0){
                        child.setPurse(0);
                    }
                    else{
                        child.setPurse(val);
                    }
                }
                else{

                    if( *(ext.getParent()) != NULL  ){
                        int val = ( *(ext.getParent()))->getPurse()  - ext.getParentBid();
                        if(val < 0){
                            child.setPurse(0);
                        }
                        else{
                            child.setPurse( val );
                        }
                    }
                    else{
                        child.setPurse(3);
                    }
                }
            if(D)
            cout << "h8" << endl;

                /* set parent bid */
                child.setParentBid(i);


            if(D)
            cout << "h9" << endl;
                /* when the child is ready with all its attributes, add it to the queue */
                myQ.push(child);
                child.printNode();
            }

            cout << endl << endl << "----Next Set of Children----" << endl;
            myQ.pop();

        }



    return 0;

    }

The program hangs at this line child.setPurse( val );I believe the value calculated by the following line

    int val = ( *(ext.getParent()))->getPurse()  - ext.getParentBid();

is wrong. Where the *(ext.getParent()) points to some garbage node. Any help will be much appreciated as I am not being able to figure this out for over 24 hours now. Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T05:22:32+00:00Added an answer on June 13, 2026 at 5:22 am

    The queue is storing objects of type node. You’re using pointers into the queue. You shouldn’t do that! When you pop from the queue, which you do at the end of each pass through your main loop, you destroy your object.

    Look:

    node &ext = myQ.front();
    
    // etc...
    
    for(int i = 0; i < ext.actions.size() ; i++){
        node &child = *(ext.actions[i]);
        child.setParent(ext);
    
        // etc...
    
        myQ.push(child);
    }
    
    myQ.pop();  // <-- POOF!!  Every pointer to 'ext' is now invalid.
    

    Every time you add to the queue, you are creating a copy of your object. When you reference that copy, you are using the queue’s internal copy. Eventually that copy will not exist. When set that pointer as each child’s parent, you are asking for serious trouble.

    The only reason this doesn’t blow up sooner is because you are leaking memory by not providing a proper destructor in node (to clean up actions). If you do implement one, you will have to also implement a copy constructor (or prevent copying by making a private, empty copy constructor).

    What you really need to do is change your queue to use pointers:

    queue<node*> myQ;
    myQ.push(&root_node);
    // etc...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an MVC application view that is generating quite a large HTML table
I have a large graph that I am generating in matplotlib. I'd like to
I am generating an on-screen figure that has two subplots: one is an image
I'm currently generating a large number (100s) of SSIS packages from C# that are
I have a large nested array that I'm generating from parsing a CSV file
I am generating a Windows batch file that might become quite large, say a
Is it just because of large API syndrome or generating random numbers that are
I'm including a rather large JAR file during compilation that I'm generating using Altova
I have long HTTP request ( generating large Excel file - about 60K records
i'm generating several large Hashmaps and putting them into 1 object on Windows (using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.