Ive been working on the project, even though its a week overdue. I just dont have alot of time to work on this. But, its written and compiles but with a lot of bugs.
The Crux is this, I create an object in a deep scope and then loose it once im out of that scope. I missed the part about objects going out of scope, but know int,char..etc variables go out of scope. Never clued in that int,char..etc are them selves special classes, so if they go then my defined class objects would go. Anyway, I went back and reviewed pointers again and "pointerized" my code, but something worries me. (that something comming in a few).
In my project I create an object in a deep scope, it needs to be avail in some outer scopes and then killed in an entirely different scope. I wrote two programs to simulate what I wanted to do and looked at thier behavior. The first code posted here uses new and creates everything on the heap/free store. There are two outer scope pointers used to reference whatever is created in the innerscope. Now, im left with a delema(still not what bothers me). I cant call delete on the objects outside the scope because they are ... well outside the scope --memory leak-- and my outer pointer will still look at the inner objects created data but that data isnt safe now that I have that the two inner pointer dangling, nor can I now delete them. Now, I added an if that deletes the inner pointers/objects in scope but now the outerpointers see trash.
In the second snipped of code here, I did exactly the same thing but stayed on the stack. Once the inner loop is done the destructor is called on the inner objects but the outer objects still point to them and can still see their data. But isnt this the stack equivalent of a stray pointer? I can see the data for now, but what keeps it from being written to by something else?
This is what is bugging me. Im missing a concept and its key to getting my project done in its current implimentation. Thanks for the time you spend on this.
first code where objects are on the heap/freestore
#include<iostream>#include<string>class test2;class test1{public: test1();~test1(); void set_val(int x); int get_val(); void set_test2(test2 *ptest2); test2 get_test2();private: int t1val; test2 *t2obj;};class test2{public:test2(); ~test2(); void set_val(int x); int get_val();private: int t2val;};using namespace std;test1::test1(){ cout<<"in test1 const"<<endl;} test1::~test1(){ cout<<"in test1 dest"<<endl;}void test1::set_val(int x){ t1val=x;}int test1::get_val(){ return t1val;}void test1::set_test2(test2 *ptest2){ t2obj=ptest2;}test2 test1::get_test2(){ return *t2obj;} test2::test2(){ cout<<"in test2 const"<<endl; } test2::~test2(){ cout<<"in test2 dest"<<endl;}void test2::set_val(int x){ t2val=x;}int test2::get_val(){ return t2val;} int main() { test1 *t1obj=new test1; t1obj=NULL; test2 *t2obj=new test2; t2obj=NULL; int outerloop=1; int innerloop=1; while (outerloop==1) { cout<<"at outerloop start"<<endl; int input; cout<<"enter 0 to stop program, or 1 to continue"<<endl; cin>>input; outerloop=input; while (innerloop==1) { cout<<"at innerloop start"<<endl; cout<<"enter a number"<<endl; cin>>input; test1 *t1=new test1; t1obj=t1; t1obj->set_val(input); cout<<"t1's val is "<<t1obj->get_val()<<endl; cout<<"enter a number"<<endl; cin>>input; test2 *t2=new test2; t2obj=t2; t2obj->set_val(input); t1obj->set_test2(t2obj); cout<<"t2's val is "<<t2obj->get_val<<endl;(); cout<<"enter 0 to stop innerloop, or 1 to stay"<<endl; cin>>input; innerloop=input; /* if (input==0) { delete t1; // <-- if this is done outside innerloop delete t2; // then it fails t1 and t2 are out of scope.. stray ptr }*/ }// end innerloop }//end outerloop cout<<"t2's val is "<<t2obj->get_val()<<endl; //t1 and t2 are dead and t1/2obj cout<<"t1's val is "<<t1obj->get_val()<<endl; // now point to trash t2obj->~test2(); t2obj=NULL; cout<<"t1's val is still "<<t1obj->get_val(); t1obj->~test1(); t1obj=NULL; delete t1obj; // if t1/2obj dest not called delete t2obj; // you get assertion failure return 0; }
second snipped of code where its all on the stack
#include<iostream>#include<string>class test2;class test1{public: test1();~test1(); void set_val(int x); int get_val(); void set_test2(test2 *ptest2); test2 get_test2();private: int t1val; test2 *t2obj;};class test2{public:test2(); ~test2(); void set_val(int x); int get_val();private: int t2val;};using namespace std;test1::test1(){ cout<<"in test1 const"<<endl;} test1::~test1(){ cout<<"in test1 dest"<<endl;}void test1::set_val(int x){ t1val=x;}int test1::get_val(){ return t1val;}void test1::set_test2(test2 *ptest2){ t2obj=ptest2;}test2 test1::get_test2(){ return *t2obj;} test2::test2(){ cout<<"in test2 const"<<endl; } test2::~test2(){ cout<<"in test2 dest"<<endl;}void test2::set_val(int x){ t2val=x;}int test2::get_val(){ return t2val;} int main() { test1 *t1obj=NULL; test2 *t2obj=NULL; int outerloop=1; int innerloop=1; while (outerloop==1) { cout<<"at outerloop start"<<endl; int input; cout<<"enter 0 to stop program, or 1 to continue"<<endl; cin>>input; outerloop=input; while (innerloop==1) { cout<<"at innerloop start"<<endl; cout<<"enter a number"<<endl; cin>>input; test1 t1; t1obj=&t1; t1obj->set_val(input); cout<<"t1's val is "<<t1obj->get_val()<<endl; cout<<"enter a number"<<endl; cin>>input; test2 t2; t2obj=&t2; t2obj->set_val(input); t1obj->set_test2(t2obj); cout<<"t2's val is "<<t2obj->get_val(); cout<<"enter 0 to stop innerloop, or 1 to stay"<<endl; cin>>input; innerloop=input; }// end innerloop }//end outerloop cout<<"t2's val is "<<t2obj->get_val()<<endl; //t1 and t2 are dead and t1/2obj cout<<"t1's val is "<<t1obj->get_val()<<endl; // still point to tt1/t2 data, but is it safe? t2obj->~test2(); t2obj=NULL; cout<<"t1's val is still "<<t1obj->get_val(); t1obj->~test1(); t1obj=NULL; return 0; }