//C++ 在堆上开辟与释放二维、三维指针
#include<iostream>
using namespace std;
int main()
{
//二级指针的开辟与释放
int number = 0;
int** p = new int*[4];
for(int i = 0; i < 4; i++) //分级定义数组大小
{
p[i] = new int[4];
}
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
p[i][j] = number++;
}
}
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
cout << p[i][j] << endl;
}
}
for(int i = 0; i < 4; i++) //分级释放堆上的指针内存
{
delete[] p[i];
}
delete []p;
int*** pp = new int**[4];
for(int i = 0; i < 4; i++) //分级定义数组大小
{
pp[i] = new int*[4];
for(int j = 0; j < 4; j++)
{
pp[i][j] = new int[4];
}
}
number = 0;
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
for(int k = 0; k < 4; k++)
{
pp[i][j][k] = number++;
}
}
}
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 4; j++)
{
for(int k = 0; k < 4; k++)
{
cout << pp[i][j][k] << " ";
}
}
}
for(int i = 0; i < 4; i++) //分级释放堆上的数组内存
{
delete []pp[i];
for(int j = 0; j < 4; j++)
{
delete []pp[i][j];
}
}
return 0;
}C++ 在堆上开辟与释放二维、三维指针
原文:http://blog.csdn.net/linukey/article/details/45276525