c++ - temporary pointer point to a using vector address -


i have question parsing string s binary tree.

struct treenode { string val;         // data in node. treenode *left;   // pointer left subtree. treenode *right;  // pointer right subtree. };  string s="((or (and pass (not reject)) (and (not pass) reject)))"; 

i stroke , eliminate "(" , ")" , keep separate part in vector aftersplit, aftersplit has (bottom) or , pass not reject , not pass reject (back)

vector<string> aftersplit; vector<treenode> parsetree; while (!aftersplit.empty())//do parsing  {       treenode *temp=new treenode;      temp->val=aftersplit.back();     temp->left=null;     temp->right=null;             aftersplit.pop_back();     if(temp->val=="and"||temp->val=="or"||temp->val=="=>"||temp->val=="<=>"){         treenode *leftnode = new treenode;           leftnode=&parsetree.back();         parsetree.pop_back();         temp->left=leftnode;         treenode *rightnode = new treenode;           rightnode=&parsetree.back();         parsetree.pop_back();         temp->right=rightnode;         parsetree.push_back(temp); //can not put temp parsetree      }     else if(temp->val=="not")     {         treenode *leftnode = new treenode;           leftnode=&parsetree.back();         parsetree.pop_back();         temp->left=leftnode;         parsetree.push_back(temp);             }     else {                     parsetree.push_back(temp);     } 

}

i deal string s right left

however when run "treenode leftnode" when operator "or"; lefenode allocated address using first "and"'s left child "pass", say, "and"'s point left children @ address 0x00007fff6d8da7e0, , new temporary leftnode's allocating address 0x00007fff6d8da7e0 after before tree like

    (and)                    / \               /   \           pass  (not)                    /                 reject   

after leftnode allocated address of "pass" like

                (and)                              /    \                         /      \                      (and)    (not)                        /    \     /                   /      \   /                     (and) (not) reject          /    \                 /      \             (and)    (not)   

and etc, point itself, know there maybe wrong pointer, can not figure out. please me

if(temp.val=="and"||temp.val=="or"||temp.val=="=>"||temp.val=="<=>"){     treenode leftnode;  //appear bug here     leftnode=parsetree.back();     parsetree.pop_back();     temp.left=&leftnode;     treenode rightnode;     rightnode=parsetree.back();     parsetree.pop_back();     temp.right=&rightnode;     parsetree.push_back(temp);   } 

your variables leftnode , rightnode have limited lifespan. since declared in scope of if, destroyed once step out of there. meaning adresses become invalid ! so, each time, content of temp.left , temp.right going point garbage data.

this why can see find same address multiple times : since previous object has been destroyed, being used store new data need. that's not want, since want preserve objects you've created.

the easiest way dynamically create treenodes need (and modify rest of code accordingly):

treenode *leftnode = new treenode; treenode *rightnode = new treenode; 

that way, stay valid after stepping out of if. must not forget delete them afterwards.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -