Thread: Deallocating Memory allocated to a pointer to a pointer

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    9

    Deallocating Memory allocated to a pointer to a pointer

    Hello. Please help me out with the following code...

    Code:
    class MATRIX
    {
    	private:
    		float **x;
    		int m,n;
    	public:
    		MATRIX();
    		MATRIX(int, int);
    		void getdata();
    		void showdata();
    		~MATRIX();
    };
    
    MATRIX::MATRIX()
    {
    	m=1; n=1; x=new float*[1]; x[0]=new float[1]; x[0][0]=0;
    }
    
    MATRIX::MATRIX(int a, int b)
    {
    	int ctr,row,col;
    	m=a; n=b;
    	x=new float*[m];
    	for(ctr=0; ctr<m; ++ctr)
    		x[ctr]=new float[n];
    	for(row=0; row<m; ++row)
    	{
    		for(col=0; col<n; ++col)
    			x[row][col]=0;
    	}
    }
    
    MATRIX::~MATRIX()
    {
    	//what do I type here?
    }
    I am unable to figure out what code I should use in the destructor to deallocate the memory allocated to the pointer to a pointer x.

    Please help out.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Like so
    Code:
    	for(ctr=0; ctr<m; ++ctr)
    		delete [] x[ctr];
    	delete [] x;

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    9
    Hey, thanks for the code. The code I submitted was actually part of a bigger problem.

    Supposing I include a friend function which returns a value of type MATRIX in the class definition, I seem to get erroneous results if I include the destructor, and the program works correctly if I do not include the destructor. Can anyone tell me why that is the case?

    For anyone who might be interested, my code is attached (it's not very long).

  4. #4
    Registered User
    Join Date
    Dec 2005
    Location
    Colchester, Essex, United Kingdom.
    Posts
    31
    Try calling showdata() directly from `mat'. Like this: mat.showdata(); It works fine for me, and besides, your assign function is completely pointless as far as I can see.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Your assign() only returns a shallow copy of the matrix, not a deep copy.

    So you end up with is either
    temp = assign(mat); // this is a shallow copy
    // destructor for mat gets called, just after the last reference to the object
    temp.showdata(); // uses the same pointer you just destroyed.
    // destructor for temp gets called, but you already deleted it

    or
    temp = assign(mat); // this is a shallow copy
    temp.showdata(); // uses the same pointer
    // destructor for mat gets called
    // destructor for temp gets called, but you already deleted it

  6. #6
    Registered User
    Join Date
    Jul 2005
    Posts
    9
    Hello tomcant. The program I attached is for demonstration purposes only. The actual function is different. I just wanted to find out where I was going wrong.

  7. #7
    Registered User
    Join Date
    Jul 2005
    Posts
    9
    Hey Salem, looks like you cleared my doubt. Thanks a lot. Now that I know what the problem is, is there any way to get around it?

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > is there any way to get around it?
    Yeah, implement proper copy and assignment functions which know how to replicate the internal structure of your class. Most decent books and tutorials should be able to provide you with the detail.

  9. #9
    Registered User
    Join Date
    Jul 2005
    Posts
    9
    Thanks Salem, that helped. The use of a copy constructor solved my problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Allocate memory inside allocated memory block?
    By Heidi_Nayak in forum C Programming
    Replies: 14
    Last Post: 04-15-2009, 04:19 PM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Will free() clean up this allocated memory?
    By simguy in forum C Programming
    Replies: 3
    Last Post: 01-18-2007, 05:21 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM