• 3733阅读
  • 4回复

这样释放内存对吗? [复制链接]

上一主题 下一主题
离线xlttap
 

只看楼主 倒序阅读 楼主  发表于: 2010-04-07
分配:
void ***alloc3 (size_t n1, size_t n2, size_t n3, size_t size)
{
   size_t i3,i2;
   void   ***p;
   if ((p=(void***)malloc(n3*sizeof(void**)))==NULL)  return NULL;
   if ((p[0]=(void**)malloc(n3*n2*sizeof(void*)))==NULL) {
      free(p);
      return NULL;
      }
   if ((p[0][0]=(void*)malloc(n3*n2*n1*size))==NULL) {
      free(p[0]);
      free(p);
      return NULL;
      }
   for (i3=0; i3<n3; i3++) {
      p[i3] = p[0] + n2*i3;
      for (i2=0; i2<n2; i2++)
         p[i3][i2] = (char*)p[0][0]+size*n1*(i2+n2*i3);
      }
   return p;
}

释放:
void free3 (void ***p)
{
   free(p[0][0]);
   free(p[0]);
   free(p);
}
我简单我快乐
离线steinlee

只看该作者 1楼 发表于: 2010-04-07
I think  you are making a 3D array.  Do the following:


template< class T>
void create3DArray( const unsigned nx, const unsigned ny, const unsigned nz, T *** & array )
{
     if ( array != NULL )
     {
          return;
     }

     unsigned i( 0 ), j( 0 );
     array = new T **[ nx ];
     for ( i = 0; i < nx; ++i )
     {
        array[ i ] = new  T*[ ny ];
     }
  
     for ( i = 0; i < nx; ++i )
     {
           for ( j = 0; j < ny; ++j )
           {
               array[ i ][ j ] = new T[ nz ];
           }
     }
}

template< class T>
void delete3DArray( const unsigned nx, const unsigned ny, T *** & array )
{
     if ( array == NULL )
     {
          return;
     }
  
     unsigned i( 0 ), j( 0 );
     for ( i = 0; i < nx; ++i )
     {
           for ( j = 0; j < ny; ++j )
           {
               delete[] array[ i ][ j ];
           }
     }

     for ( i = 0; i < nx; ++i )
     {
        delete[] array[ i ];
     }

     delete[] array; array = NULL;
}

Note that do not forget []  for delete. Otherwise, there is memory leak. You need to know why.
[ 此帖被steinlee在2010-04-07 23:13重新编辑 ]
Looking for remote C/C++ and Qt 兼职
离线xlttap

只看该作者 2楼 发表于: 2010-04-07
[quote]引用楼主xlttap于2010-04-07 10:28发表的 这样释放内存对吗?

分配:
void ***alloc3 (size_t n1, size_t n2, size_t n3, size_t size)
{
   size_t i3,i2;
   void   ***p;
   if ((p=(void***)malloc(n3*sizeof(void**)))==NULL)  return NULL;
   if ((p[0]=(void**)malloc(n3*n2*sizeof(void*)))==NULL) {
      free(p);
      return NULL;
      }
   if ((p[0][0]=(void*)malloc(n3*n2*n1*size))==NULL) {
      free(p[0]);
      free(p);
      return NULL;
      }
   for (i3=0; i3<n3; i3++) {
      p[i3] = p[0] + n2*i3;
      for (i2=0; i2<n2; i2++)
         p[i3][i2] = (char*)p[0][0]+size*n1*(i2+n2*i3);
      }
   return p;
}

释放:
void free3 (void ***p)
{
   free(p[0][0]);
   free(p[0]);
   free(p);
}
这个代码是我们老板(几十年开发经验)写的,我没看懂他为什么这样释放,所以了上来问问。
我简单我快乐
离线steinlee

只看该作者 3楼 发表于: 2010-04-07
Check my code again. I made a little change. One place was not displayed properly.

Experience helps and is not everything you can count on.
The best coders are the guys who make fewer mistakes and are able to find and fix bugs quickly.
Looking for remote C/C++ and Qt 兼职
离线午小夜

只看该作者 4楼 发表于: 2010-04-08
do you know the difference between
  1. object   *p   =   new   object[20];  
  2.   delete   []p; 

and
  1.   object   *p   =   new   object();  
  2.   delete   p;
  
?
[ 此帖被午小夜在2010-04-08 00:14重新编辑 ]
[操作系统版本]  Windows XP;Linux Ubuntu;Linux Fedora;
[Qt SDK版本]    4.7.0
[SDK 发布日期]  2010.05
[IDE(集成开发环境)] QtCreator
个人网页:http://hi.baidu.com/午小夜
學歷:Royal Jalidon
快速回复
限100 字节
 
上一个 下一个