C++ 在C+中的2D动态内存分配数组中释放分配的内存+;

C++ 在C+中的2D动态内存分配数组中释放分配的内存+;,c++,multidimensional-array,dynamic-memory-allocation,C++,Multidimensional Array,Dynamic Memory Allocation,我最近发布了一个关于这个的问题,但这是一个不同的问题。我使用动态内存分配创建了一个2D数组,使用矩阵后,我们需要通过删除它来释放内存,我不明白为什么我们不能使用delete[]matrix来删除它,而不是使用下面代码中的方法 int **matrix; // dynamically allocate an array matrix = new int *[row]; for (int count = 0; count < row; count++)

我最近发布了一个关于这个的问题,但这是一个不同的问题。我使用动态内存分配创建了一个2D数组,使用矩阵后,我们需要通过删除它来释放内存,我不明白为什么我们不能使用
delete[]matrix
来删除它,而不是使用下面代码中的方法

int **matrix;

    // dynamically allocate an array
    matrix = new int *[row]; 
    for (int count = 0; count < row; count++)
        matrix[count] = new int[col];

    // free dynamically allocated memory
    for( int i = 0 ; i < *row ; i++ )
    {
        delete [] matrix[i] ;
        delete [] matrix ;
    }

您必须遍历矩阵并删除每个数组。完成此操作后,可以删除矩阵本身

// free dynamically allocated memory
for( int i = 0 ; i < *row ; i++ )
{
    delete[] matrix[i]; // delete array within matrix
}
// delete actual matrix
delete[] matrix;
//释放动态分配的内存
对于(int i=0;i<*行;i++)
{
delete[]矩阵[i];//删除矩阵中的数组
}
//删除实际矩阵
删除[]矩阵;

您必须检查矩阵并删除每个数组。完成此操作后,可以删除矩阵本身

// free dynamically allocated memory
for( int i = 0 ; i < *row ; i++ )
{
    delete[] matrix[i]; // delete array within matrix
}
// delete actual matrix
delete[] matrix;
//释放动态分配的内存
对于(int i=0;i<*行;i++)
{
delete[]矩阵[i];//删除矩阵中的数组
}
//删除实际矩阵
删除[]矩阵;

如果您使用的是动态数组,我强烈建议您使用std::vector。性能损失几乎为零,考虑到您可以更优雅地将std算法与向量结合使用,您的代码很可能最终会获得更高的性能

unsigned int cols=40, rows=35;
std::vector<int> temp(cols,0); //this is only created so that we can initialize a 
                               //row at a time, the first parameter is the amount of 
                               //elements to initialize with and the second is the value
                               //to initialize with
std::vector<std::vector<int>> matrix(rows,temp); //creates a vector with 35 vectors each
                                                 //initialized with the values in temp

matrix[2][3] = 4;              //use just like a normal array
matrix.resize(88,temp);         //you can add rows later too
matrix.push_back(temp);         //or like this

//probably the most important, you don't need to delete because you never needed to 
//use new in the first place
unsigned int cols=40,rows=35;
标准:向量温度(cols,0)//这只是为了我们可以初始化
//行,第一个参数是
//元素进行初始化,第二个是值
//初始化
std::向量矩阵(行、温度)//创建一个向量,每个向量包含35个向量
//使用temp中的值初始化
矩阵[2][3]=4//像普通数组一样使用
调整矩阵大小(88,温度)//以后也可以添加行
矩阵。推回(温度)//还是像这样
//也许最重要的是,你不需要删除,因为你从来都不需要删除
//首先使用新的

使用new和delete并不是真正的现代风格,这是有充分理由的。根据C++的Gururs和超越惯例,它只应该在性能优化的情况下使用,并且在编写库代码时使用。许多书和老师仍然这样教,但另一方面,大多数书都是垃圾。以下是其中的精华:

如果您使用的是动态数组,我强烈建议您使用std::vector。性能损失几乎为零,考虑到您可以更优雅地将std算法与向量结合使用,您的代码很可能最终会获得更高的性能

unsigned int cols=40, rows=35;
std::vector<int> temp(cols,0); //this is only created so that we can initialize a 
                               //row at a time, the first parameter is the amount of 
                               //elements to initialize with and the second is the value
                               //to initialize with
std::vector<std::vector<int>> matrix(rows,temp); //creates a vector with 35 vectors each
                                                 //initialized with the values in temp

matrix[2][3] = 4;              //use just like a normal array
matrix.resize(88,temp);         //you can add rows later too
matrix.push_back(temp);         //or like this

//probably the most important, you don't need to delete because you never needed to 
//use new in the first place
unsigned int cols=40,rows=35;
标准:向量温度(cols,0)//这只是为了我们可以初始化
//行,第一个参数是
//元素进行初始化,第二个是值
//初始化
std::向量矩阵(行、温度)//创建一个向量,每个向量包含35个向量
//使用temp中的值初始化
矩阵[2][3]=4//像普通数组一样使用
调整矩阵大小(88,温度)//以后也可以添加行
矩阵。推回(温度)//还是像这样
//也许最重要的是,你不需要删除,因为你从来都不需要删除
//首先使用新的

使用new和delete并不是真正的现代风格,这是有充分理由的。根据C++的Gururs和超越惯例,它只应该在性能优化的情况下使用,并且在编写库代码时使用。许多书和老师仍然这样教,但另一方面,大多数书都是垃圾。以下是其中的精华:

因为int*类型没有析构函数,至少它没有您想要的析构函数。通过这样做,您可以避免所有内存分配/释放的事情

std::vector<int*> matrix(rows);
std::vector<int> allData(rows * cols);
for(int i = 0; i < rows; i++)
{
matrix[i] = &allData[i * cols];
}
std::向量矩阵(行);
std::矢量所有数据(行*列);
对于(int i=0;i

或者使用标准容器,如boost::numeric::ublas::matrix。

因为int*类型没有析构函数,至少它没有您想要的析构函数。通过这样做,您可以避免所有内存分配/释放的事情

std::vector<int*> matrix(rows);
std::vector<int> allData(rows * cols);
for(int i = 0; i < rows; i++)
{
matrix[i] = &allData[i * cols];
}
std::向量矩阵(行);
std::矢量所有数据(行*列);
对于(int i=0;i

或者使用标准容器,如boost::numeric::ublas::matrix。

那么,如果我只写
delete[]matrix?@user1986597您将泄漏为矩阵中所有阵列分配的内存。狮子托尼是对的,在严格的场景中,这是正确的答案,我应该接受。谢谢,它工作得很好,因此,将delete实际矩阵始终放在循环中是错误的?那么,如果我只写
delete[]矩阵,会发生什么
?@user1986597您将泄漏为矩阵中所有数组分配的内存。Tony the Lion是对的,在严格的场景中,这是正确的答案,应该在我的脑海中被接受。感谢它工作得很好,所以将删除实际矩阵放在循环中始终是错误的?我同意,他们应该用现代风格来教学生基本的材料,而不是像这样的旧东西!!!我同意,他们应该用现代风格来教学生基本的材料,而不是像这样的旧东西!!!