删除多维数组 [英] delete multidimensional arrays

查看:126
本文介绍了删除多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++常见问题中,[16.16]给出了以下示例:

  void manipulateArray(unsigned nrows,unsigned ncols [ )
{
typedef Fred * FredPtr;
FredPtr * matrix = new FredPtr [nrows];

//如果稍后有异常,将每个元素设置为NULL。
//(unsigned i = 0; i matrix [i] = NULL ;
try {
for(unsigned i = 0; i matrix [i] = new Fred [ncols [i]];
for(unsigned i = 0; i for(unsigned j = 0; j< ncols [i]; ++ j){
someFunction (matrix [i] [j]);
}
}

if(today ==Tuesday&&& moon.isFull()){
for(unsigned i = nrows; i& ; 0; --i)
delete [] matrix [i-1];
delete [] matrix;
return;
}

...使用矩阵...的代码...

}
catch(...){
(unsigned i = nrows; i> 0; -i)
delete [] matrix [i-1];
delete [] matrix;
throw; //重新抛出当前异常
}

for(unsigned i = nrows; i> 0; --i)
delete [] matrix [i-1] ;
delete [] matrix;
}

为什么我们要用这种方式删除,

第一 delete [] matrix [i-1];
then delete [] matrix ;



此外,在整个try ... catch循环之后,我们还需要放置

  for(unsigned i = nrows; i> 0; --i)
delete [] matrix [i-1]
delete [] matrix;

解决方案


  1. 为了确保正确的清理, try / catch 异常在正常清理发生之前在代码中的任何位置抛出。这包括 new 表达式之一中的异常。 delete [] 是安全的,因为所有相关的指针最初设置为零,即使没有发生分配,删除也是有效的。



    (注意,如果发生任何异常,它仍会在函数之外传播。局部 try / catch


  2. 有两组数组:一组是外层数组 matrix ,这是一个指针数组。此数组首先被分配,最后被删除。第二,每个元素 matrix [i] 本身是指向 Fred 元素。每个数组在第一个中分配给循环,因此必须在末尾的另一个循环中删除。



    1. In C++ FAQ, the [16.16] gives the following example,

      void manipulateArray(unsigned nrows, unsigned ncols[])
      {
         typedef Fred* FredPtr;
         FredPtr* matrix = new FredPtr[nrows];
      
         // Set each element to NULL in case there is an exception later.
         // (See comments at the top of the try block for rationale.)
         for (unsigned i = 0; i < nrows; ++i)
             matrix[i] = NULL;
         try {
           for (unsigned i = 0; i < nrows; ++i)
           matrix[i] = new Fred[ ncols[i] ];
           for (unsigned i = 0; i < nrows; ++i) {
              for (unsigned j = 0; j < ncols[i]; ++j) {
               someFunction( matrix[i][j] );
               }
            }
      
           if (today == "Tuesday" && moon.isFull()) {
             for (unsigned i = nrows; i > 0; --i)
              delete[] matrix[i-1];
             delete[] matrix;
             return;
           }
      
       ...code that fiddles with the matrix...
      
          }
        catch (...) {
           for (unsigned i = nrows; i > 0; --i)
            delete[] matrix[i-1];
            delete[] matrix;
             throw;    // Re-throw the current exception
         }
      
        for (unsigned i = nrows; i > 0; --i)
           delete[] matrix[i-1];
           delete[] matrix;
        }
      

      Why we have to use delete this way, I mean,

      First delete[] matrix[i-1]; then delete[] matrix;

      Moreover, what’s the point of after the whole "try…catch" cycle, we still have to put

      for (unsigned i = nrows; i > 0; --i)
         delete[] matrix[i-1];
         delete[] matrix;
      

      at the end of this function.

      解决方案

      1. The try/catch block is necessary to ensure proper clean-up even if an exception is thrown anywhere in the code before the normal clean-up happens. This includes an exception in one of the new expressions. The delete[] is safe because all the relevant pointers were initially set to zero, so that the deletion is valid even if no allocation ever occurred.

        (Note that if any exception does occur, it will still be propagated outside the function. The local try/catch block only ensures that the function itself doesn't leak any memory.)

      2. There are two sets of arrays: one is the outer array matrix, which is an array of pointers. This array gets allocated first and deleted last. Second, each element matrix[i] is itself a pointer to an array of Fred elements. Each array gets allocated in the first for loop, and thus has to be deleted in another loop at the end.

      这篇关于删除多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆