三維指標-記憶配置與刪除(.net2005編譯)
#include <stdio.h>
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
int length = 4, width = 5, depth = 6;
char *** video;
video = new char**[length];
// allocate the memory storage
for(int i=0; i<length; i++)
{
video[i] = new char*[width];
for(int j=0; j<width; j++)
video[i][j] = new char[depth];
}
// not needed, just set values to video[][][]
for(int i=0; i<length; i++)
for(int j=0; j<width; j++)
for(int h=0; h<depth; h++)
video[i][j][h] = i+j+h;
// delete the memory storage
for(int i=0; i<length; i++)
{
for(int j=0; j<width; j++)
delete [] video[i][j];
delete [] video[i];
}
delete [] video;
// set the pointer ***video to "NULL"
video = NULL;
return 0;
}
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
int length = 4, width = 5, depth = 6;
char *** video;
video = new char**[length];
// allocate the memory storage
for(int i=0; i<length; i++)
{
video[i] = new char*[width];
for(int j=0; j<width; j++)
video[i][j] = new char[depth];
}
// not needed, just set values to video[][][]
for(int i=0; i<length; i++)
for(int j=0; j<width; j++)
for(int h=0; h<depth; h++)
video[i][j][h] = i+j+h;
// delete the memory storage
for(int i=0; i<length; i++)
{
for(int j=0; j<width; j++)
delete [] video[i][j];
delete [] video[i];
}
delete [] video;
// set the pointer ***video to "NULL"
video = NULL;
return 0;
}