C++ 取消所有动态分配的多维数组。

C++ 取消所有动态分配的多维数组。,c++,arrays,heap,dynamic-arrays,memory-management,C++,Arrays,Heap,Dynamic Arrays,Memory Management,所以,我有这个代码,我试图在最后释放数组ppint。我曾尝试使用Xcode泄漏来确定它是否工作,但我不太理解它。做这个行吗 delete ppint[0]; delete ppint[1]; delete ppint[2]; delete ppint[3]; 还是必须做些别的事情 #include <iostream> #include <string> #include <unistd.h> using namespace std; int main()

所以,我有这个代码,我试图在最后释放数组
ppint
。我曾尝试使用Xcode泄漏来确定它是否工作,但我不太理解它。做这个行吗

delete ppint[0];
delete ppint[1];
delete ppint[2];
delete ppint[3];
还是必须做些别的事情

#include <iostream>
#include <string>
#include <unistd.h>
using namespace std;

int main()
{
    int **ppint;
    ppint = new int * [4];

    for(int i = 0; i < 4; i++ ) {
        ppint [i] = new int[4];
    } // declares second layer of arrays

    for(int i = 0, count = 0; i < 4; i++ ) {
        for(int j = 0; j < 4; j++ ) {
            count++;
            ppint [i] [j] = count;
        } //init part 2
    } // init array

    for(int i = 0; i < 4; i++ ) {
        for(int j = 0; j < 4; j++ ) {
            cout << ppint [i] [j] << endl;
        } // print part 2
    } //print array
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
int**ppint;
Pint=新整数*[4];
对于(int i=0;i<4;i++){
ppint[i]=新整数[4];
}//声明数组的第二层
for(int i=0,count=0;i<4;i++){
对于(int j=0;j<4;j++){
计数++;
ppint[i][j]=计数;
}//初始化第2部分
}//初始化数组
对于(int i=0;i<4;i++){
对于(int j=0;j<4;j++){

cout关闭。您必须使用
delete[]
,因为您为它们分配了
new[]

delete[] ppint[0];
delete[] ppint[1];
delete[] ppint[2];
delete[] ppint[3];
但当然,您应该使用循环:

for(int i = 0; i < 4; i++ ) {
  delete[] ppint[i];
}

<>但是,在C++中,我们更喜欢不要使用动态分配的数组。使用<代码>:STD::向量::数组。如果你关心数据的局部性,尝试.< /P> < P>你仍然需要删除由代码“pPcT< /COD>本身”指向的内存。你还需要使用<代码>删除[]/COD> >而不是<代码>删除< /代码> .< 但是,与手动阵列相比,更喜欢标准容器

std::vector< std::vector<int> > iv; // dynamic size
std::array< std::array<int,4>, 4> ia; // static size
std::vectoriv;//动态大小
std::arrayia;//静态大小

使用
new[]
分配的内容需要处理
delete[]
。但是使用
std::vector
而不是原始数组和
new
。它会自动为您管理内存


以下内容直接模拟您的原始代码,并且更短、更干净、更安全:

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<vector<int>> v( 4, vector<int>( 4 ) );

    for( int i = 0, count = 0; i < 4; ++i ) {
        for( int j = 0; j < 4; ++j ) {
            ++count;
            v[i][j] = count;
        }
    }

    for( int i = 0; i < 4; ++i ) {
        for( int j = 0; j < 4; ++j ) {
            cout << v[i][j] << endl;
        }
    }
}
#包括
#包括
使用名称空间std;
int main()
{
向量v(4,向量(4));
对于(int i=0,count=0;i<4;++i){
对于(int j=0;j<4;++j){
++计数;
v[i][j]=计数;
}
}
对于(int i=0;i<4;++i){
对于(int j=0;j<4;++j){

cout对于以前每次调用
new

for(int i = 0; i < 4; i++ ) {
    delete[] ppint[i];
}
delete[] ppint;
for(int i=0;i<4;i++){
删除[]ppint[i];
}
删除[]ppint;
我的解决方案是:

#include<iostream>
#include<cstdlib>

using namespace std;

int main(){

    int ** twod;
    twod = new int*[4];
    int counter = 0;
    /*init 2d variable and check whether we got the memory*/
    if ( twod == NULL) {
        exit(EXIT_FAILURE);
    }
    for (unsigned i = 0; i< 4; i++){
        /**/
        twod[i] = new int[4];
        if (twod[i] == NULL){
            exit(EXIT_FAILURE);
        }
        for (unsigned j = 0; j < 4; j++){
            counter++;
            twod[i][j]=counter;
        }
    }

    for ( unsigned i = 0; i < 4; i++){
        for (unsigned j = 0; j < 4; j++){
            cout << twod[i][j] << endl ;
        }
    }

    for (unsigned i = 0; i < 4; i++)
        delete [] twod[i];

    /*and don't forget to delete the int* array as well.*/
    delete [] twod;

}

当然还有其他人在使用STD::向量,例如,你在C++中编码而不是C.< /P>不使用STD的任何特殊原因::vector?@ PIOKUC,我在一本书中工作,这样说,除了我没有使用向量的事实之外,只有真正的原因。记住:每个<代码>新< /代码>应该有对应的

删除
,每个
新[]
都应该有一个相应的
删除[]
。去寻找向量向量。你会花一些时间学习,但这是值得的……重新
删除ppint;
,分配给
新[]
的东西需要处理
删除[]
。你说得对。我的代码确实产生了不同的输出,但我试图将注意力集中在OP的内存问题上。我调整了答案,以获得与OP相同的输出
for(int i = 0; i < 4; i++ ) {
    delete[] ppint[i];
}
delete[] ppint;
#include<iostream>
#include<cstdlib>

using namespace std;

int main(){

    int ** twod;
    twod = new int*[4];
    int counter = 0;
    /*init 2d variable and check whether we got the memory*/
    if ( twod == NULL) {
        exit(EXIT_FAILURE);
    }
    for (unsigned i = 0; i< 4; i++){
        /**/
        twod[i] = new int[4];
        if (twod[i] == NULL){
            exit(EXIT_FAILURE);
        }
        for (unsigned j = 0; j < 4; j++){
            counter++;
            twod[i][j]=counter;
        }
    }

    for ( unsigned i = 0; i < 4; i++){
        for (unsigned j = 0; j < 4; j++){
            cout << twod[i][j] << endl ;
        }
    }

    for (unsigned i = 0; i < 4; i++)
        delete [] twod[i];

    /*and don't forget to delete the int* array as well.*/
    delete [] twod;

}
$ valgrind ./a.out
==18376== Memcheck, a memory error detector
==18376== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==18376== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==18376== Command: ./a.out
==18376== 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
==18376== 
==18376== HEAP SUMMARY:
==18376==     in use at exit: 0 bytes in 0 blocks
==18376==   total heap usage: 5 allocs, 5 frees, 96 bytes allocated
==18376== 
==18376== All heap blocks were freed -- no leaks are possible
==18376== 
==18376== For counts of detected and suppressed errors, rerun with: -v
==18376== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)