CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2011
    Posts
    21

    what's wrong with delete [] a[0];

    Hi all ,
    I wrote a program, but there is a problem, it reminds "debug error!", I have found that the problem was in "ginv:: ~ginv (){}" but I don't know how to solve it,can anyone help me with this, thank you !
    Code:
    void CCalginvView::OnCalginv() 
    {
    	// TODO: Add your command handler code here
    	ginv  uv(5, 4);
    	uv.input ();         //由文件读入矩阵A的元素以及eps
    	uv.a_ginv ();           //计算广义逆
    	uv.output ();        //输出U矩阵和V矩阵以及奇异值
    
    }
    Code:
    //1GINV.h
      #ifndef GINV_H_H
      #define GINV_H_H
    
      #include  <iostream>
      #include  <fstream>
      #include  <cmath>
      using namespace std;
      class  ginv
      {
      private: 
    	  int m, n, ka;
    	  double  **a, **u, **v, **aa, eps;
    	  double  *s, *e, *w, fg[2], cs[2];
    	
      public:
    	 ginv (int mm, int nn);
                     void input ();        
    	 void uav ();          //svd
               void ppp();            
               void sss();            
               void a_ginv ();         
    	 void output ();        
    	 ~ginv ();
       };
      #endif
    Code:
     //1GINV.CPP
      #include "stdafx.h"
      #include  <iostream.h>
      #include  <fstream.h>
      #include  <math.h>
      #include "ginv.h"
    
    
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
      static char THIS_FILE[] = __FILE__;
    #endif
    ginv::ginv (int mm, int nn)
    	{
    			   int i;
    			   m = mm; n= nn;
    	           a = new double*[m];   //动态分配内存空间
    	           for (i=0; i<m; i++) a[i] = new double[n];
    	           u = new double*[m];
    	           for (i=0; i<m; i++) u[i] = new double[m];
    	           v = new double*[n];
    	           for (i=0; i<n; i++) v[i] = new double[n];
    	           aa = new double*[n];
    	           for (i=0; i<n; i++) aa[i] = new double[m];
                   ka = m + 1;
    	           if (m < n)  ka = n + 1;
                   s = new double[ka];
                   e = new double[ka];
                   w = new double[ka];
    	 }	
    
    ginv:: ~ginv ()
    	{
    	int i;
    	
    	for (i=0; i<m; i++)	{ delete []a[i]; }
    	delete [] a;
    	for (i=0; i<m; i++) { delete [] u[i]; }
    	delete [] u;
    	for (i=0; i<n; i++) { delete [] v[i]; }
    	delete [] v;
    	for (i=0; i<n; i++) { delete [] aa[i]; }
    	delete [] aa;
    		delete [] s, e, w;
    	 }
               ..............

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: what's wrong with delete [] a[0];

    Quote Originally Posted by Angela2010 View Post
    Hi all ,
    I wrote a program, but there is a problem, it reminds "debug error!", I have found that the problem was in "ginv:: ~ginv (){}" but I don't know how to solve it,can anyone help me with this, thank you !
    For the sake of others, here is full, runnable version of your program:
    Code:
    class  ginv
    {
      private: 
    	  int m, n, ka;
    	  double  **a, **u, **v, **aa, eps;
    	  double  *s, *e, *w, fg[2], cs[2];
    	
      public:
    	 ginv (int mm, int nn);
             void input ();        
    	 void uav ();          //svd
             void ppp();            
             void sss();            
             void a_ginv ();         
    	 void output ();        
    	 ~ginv ();
    };
    
    
    ginv::ginv (int mm, int nn)
    {
       int i;
       m = mm; n= nn;
       a = new double*[m];   //动态分配内存空间
       for (i=0; i<m; i++) 
            a[i] = new double[n];
    
       u = new double*[m];
       for (i=0; i<m; i++) 
            u[i] = new double[m];
    
       v = new double*[n];
       for (i=0; i<n; i++) 
           v[i] = new double[n];
    
       aa = new double*[n];
       for (i=0; i<n; i++) 
            aa[i] = new double[m];
    
       ka = m + 1;
       if (m < n)  
              ka = n + 1;
       s = new double[ka];
       e = new double[ka];
       w = new double[ka];
    }	
    
    ginv:: ~ginv ()
    {
    	int i;
    	
    	for (i=0; i<m; i++)	
                { delete []a[i]; }
    	delete [] a;
    
    	for (i=0; i<m; i++) 
                { delete [] u[i]; }
    	delete [] u;
    
    	for (i=0; i<n; i++) 
                { delete [] v[i]; }
    
    	delete [] v;
    	for (i=0; i<n; i++) 
                { delete [] aa[i]; }
    
    	delete [] aa;
    	delete [] s, e, w;
    }
    
    int main()
    {
        ginv  uv(5, 4);
    /*    uv.input (); 
        uv.a_ginv ();
        uv.output (); */
    }
    First, where is the input() function? Where is the output function? Where is the a_ginv() function? You're calling all of these functions, and we have no idea if these functions are causing the problem.

    Second, what is this line supposed to do?
    Code:
    	delete [] s, e, w;
    What book or tutorial shows you that this is the way to deallocate multiple data items using "delete"? The delete works on one item at a time, not multiple items on the same line. This code does things that you don't expect it to do.

    Third, you could easily have just used CArray or std::vector instead of all of these allocations/deallocations. Then you're problems would have disappeared.

    Last, your ginv class lacks a proper user-defined copy constructor and assignment operator. I can easily break your code with a three line program:
    Code:
    int main()
    {
       ginv gv1(2,2);
       ginv gv2(3,3);
       gv1 = gv2;
    }
    That three line program has a memory leak, plus it's calling delete on the same pointer value more than once when gv1 and gv2 are destroyed.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; December 24th, 2011 at 02:52 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured