Thread: c++ code a destructor to delete the dynamically allocated array!!!

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    22

    c++ code a destructor to delete the dynamically allocated array!!!

    In my code I have to define a class called Array, it simulates a dynamic array of integers and I also have to code a constructor. This all seems to work fine, but I also have to code a destructor that will delete the dynamically allocated array, this is where I run into problems. This is the code that I have.

    Code:
    ____________________________________________________
    class Array
    {
    	private:
    		int size;
    		int *pointer;
    		
    	public:
    	   Array()
    	   {
    	       size=0;
                           pointer=NULL;
    	  }
    
    	void load()
    	{
    	    cout <<"Enter the number of integers in the array.\n\n";
    	    cin >>size;
    	    pointer = new int[size];
    	    int i;
    	    for (i=0; i< size; i++)
    	    {
    	        cout <<"Enter interger # " << i+1 <<"\n";
    	        cin >>*(pointer+i);
    				
    				
    	   }
                     }//end of load
    
    	void print()
    	{	
    	   int i =0;
    	   cout <<"The array contains " <<size << "integers.\n\n";
    	   cout <<"They are:\n";
    	    while (i < size)
    	       {
    	           cout <<i+1<<")\t"<<*(pointer+i)<<endl;
    	            i++;
    
    	       }//end of loop
    
    	  }
    
    	~Array()        //destructor to delete the array
    	{
    		delete []pointer;
    	}
    
    };
    
    void main (void)
    {
    	Array array1;
    	array1.load();
    	array1.print();
    	array1.~Array();
    	
    
    	
    }
    
    __________________________________________________

    It runs fine when I comment out the destructor, but with it in my program crashes after it displays the integers in the array. Any help would be great. Thanks

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Because you're explicitly calling your destructor.

    What's happening is it's deallocating the memory when you explicitly call it, then when the destructor is called automatically at the end of main it tries to delete the memory again. Solution -- Don't explicitly call the destructor, it being automatic is the whole point of a destructor!

  3. #3
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    you should call

    delete array1;

    but as was previouslt said, it doesn't really matter because the program exits right after it anyway, so the memory will be deallocated without calling anything.

    but in most programs you should use delete, it will call the deconstructor properly.

  4. #4
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by beege31337
    you should call

    delete array1;
    No, you shouldn't be. You can only dynamically deallocate memory through a pointer which points to memory which was dynamically allocated!

    In fact, since it's not even a pointer the code wouldn't even compile if you did that.

  5. #5
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    oh yeah, sorry I wasn't really paying attention.

  6. #6
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    try:
    Code:
    int main()
    {
    	Array array1;
    	array1.load();
    	array1.print();
    	//array1.~Array(); //take this out, the class automatically deconstructs it
    	return 0;
    }
    //plz include the int!
    the deconstructor automatically executes after the program or class is done
    what you have done is basically:
    Code:
    delete [] ptr;
    //then
    delete [] ptr; //again
    and this is like a HUGE OOPS!!!
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamically allocated array of an abstract type
    By Zerok in forum C++ Programming
    Replies: 4
    Last Post: 11-24-2008, 06:50 AM
  2. Problem with Dynamically Increasing Array of Integers
    By laserlight in forum C++ Programming
    Replies: 30
    Last Post: 07-04-2008, 07:27 AM
  3. Dynamically Allocated Array
    By vb.bajpai in forum C Programming
    Replies: 3
    Last Post: 06-17-2007, 08:40 AM
  4. Trying to make this code faster & Cramer
    By just2peachy in forum C++ Programming
    Replies: 3
    Last Post: 12-03-2004, 10:54 AM
  5. Passing dynamically allocated array to Templated Class
    By MrWizard in forum C++ Programming
    Replies: 3
    Last Post: 04-01-2002, 01:36 PM