c++ - How to get around the dynamically allocated variables in a loop? -


i beginner in c++. have question. in order make code work, dynamically allocated 2 variables: 1 outside loop, , other inside loop. here code: ///////////////////////////////////////////////////////////////////////////

tree* tree0 = new tree(); (int i=0; i<1000; i++) {  tree* tree1 = new tree();  ...........  1st: insert tree0;  ...........  2nd: read nodes tree0, something, write nodes tree1;  ...........  3rd: tree0 = tree1;  } tree0->print(); 

//////////////////////////////////////////////////////////////////////

i have use "= new tree()" dynamically define variables, otherwise, have segmentation fault; have dynamically define tree1 inside loop, otherwise, segmentation fault; don't know how delete or release memory space. therefore, when application problem becomes bigger , bigger, run out of memory...could me around issue without affecting function.

thanks in advance : )

you seem missing delete keyword. delete frees memory of object performint other tasks outlined object's class destructor (in case ~tree())

try this:

tree* tree0 = new tree(); (int i=0; i<1000; i++) {  tree* tree1 = new tree();  ...........  1st: insert tree0;  ...........  2nd: read nodes tree0, something, write nodes tree1;  ...........  3rd: need delete tree0; here  4th: tree0 = tree1;  } tree0->print(); delete tree0; // clean last tree. 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -