about STL for dynamically allocating memory...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ayan4u
    New Member
    • Apr 2007
    • 86

    about STL for dynamically allocating memory...

    i have class Sum_class defines as...
    Code:
    class Sum_class
    {
    private:
    	 int data_member1;
    	 char* data_member2;
    public:
    	 Sum_class()
    	 { data_member2 = new char;
    	 }
    	 ~Sum_class()
    	 { cout<<"Destructor called";
    		 delete data_member2;
    		 data_member2=0;
    	 }
    	 void get_input(void)
    	 {
    	 code.....
    	 }		
    	 void show_output(void)
    	 { 
    	 code....
    	 }
    };
     
    int main()
    {
    	 vector<Sum_class> object;
    	 char CONTINUE ='y';
    	while(CONTINUE!='n' || CONTINUE!='N')
    {
    		 object.push_back(Sum_class()); // this will invoke constructor
    		 ....
    		 code...
    }
    ....
    code...
     
    return EXIT_SUCCESS;
    }
    the problem is that the destructor will be invoked soon after the constructor is called as the objects scope are localised within the loop...

    is there any way out so that i can declare objects dynamically using vector (as many as want)...and prevent the call of destructor.....
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Hi,
    If u make a change in the code then the destructor wont be called.
    Check this code
    Code:
    int main()
    {
    	vector<Sum_class*> object;
    	char CONTINUE ='y';
    	Sum_class *obj=NULL;
    	while(CONTINUE!='n' || CONTINUE!='N')
    	{
    		obj=new Sum_class();
    		//object.push_back(Sum_class()); // this will invoke constructor
    		object.push_back(obj);	}
    	return 0;
    }
    Thanks
    Raghuram

    Comment

    • ayan4u
      New Member
      • Apr 2007
      • 86

      #3
      ohhhhhhhh...how could i forget pointers...thnx ...thnx..thnx.. .a lot....i forgot tht we can as well use pointers with STL.....thnx again

      Comment

      • ayan4u
        New Member
        • Apr 2007
        • 86

        #4
        i thnk i was overwhelmed and thanked u too much(which i shldnt as i walked through ur suggestions)... ..u r idea dd solved the problem apparently(that it didnot call the destructor of the vector objects but for the temporary object obj) it itself came up with too many bugs.....for instance i am dynamically assigning memory for one of data members....so when i am invoking object.push_bac k(obj) i am actually invokin the default copy constructor.... .i thnk the solution lies in an explict copy construtor.....

        with your method i am gettn a warning:HEAP while debuggn.....


        so i thnk custom made copy constructor is the answer.....wat say...

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Your problem is not an explicit copy consturctor.

          Instead it is no copy constructor at all.

          When you have no copy constructor, the compiler trots out its default constructor. This default copy constructor just does memberwise copy if your class data memebers. Specifically, it copies data_member2. Afterwards, the original object points to the same char as the copy. Whichever object calls its destructor, the data_member2 is deleted. Then when the other object calls its destructor you crash trying to delete data that already has been deleted.

          Generally, classes wwith pointers as member require a copy constructor:

          Code:
          Sum_class::Sum_class(const Sum_class& orig)
          {
          this->data_member2 = new char;
          *this->data_member2 = *orig.data_member2;
          }
          Now the copy object has a copy of the original object's data_member2 and each object can now call its destructor without messing things up.

          As a further note:

          The worst thing you can do in a C++ program is pass pointers around. You cannot safely delete them unless you know there are no orther copies of the pointer in the program. I suggest you learn to use an Envelope class, sometimes canned a handle. The handle class can keep track ofd the number of instances of the pointer that still exist. Check Design Patterns. Look up the Bridge Pattern.

          Comment

          • gpraghuram
            Recognized Expert Top Contributor
            • Mar 2007
            • 1275

            #6
            Hi Ayan,
            I gave u the solution with whatever info u have asked for and i was not expecting a thanks from u for the same.
            Explain the problem clearly so that people can give u right solutions and get thanks from you.
            Thanks
            Raghuram

            Comment

            • ayan4u
              New Member
              • Apr 2007
              • 86

              #7
              neways the problem is now solved....all i had to do is make few changes arnd....nonethe less ur suggestions are valuable....

              Comment

              Working...