不知道楼主的具体想法,下面的代码不知道是不是符合楼主的意思#include <iostream>
#include <windows.h>
#define X 2
#define Y 2
#define Z 2using namespace std;int main(int argc, char* argv[])
{
/*   unsigned int x = 100;
  int w = -200;
(w+(int)x>100)?printf(">100 \n"):printf("<100 \n");
 printf("%d \n",x+w);
 cout<<w+(int)x <<"\n"<<x<<"\n"<<w;
 cout<<endl;
// ::printf("%d \n",sizeof(int));*/ int arr[X][Y][Z];
int i,j,k;
::SecureZeroMemory(arr,X*Y*Z*sizeof(int));
cout<<"start output size of data is "<<sizeof(int)<<endl;
for(i=0;i<X;i++)
{
for(j=0;j<Y;j++)
{
for(k=0;k<Z;k++)
cout<<arr[i][j][k]<<endl;
}
} return 0;
}我这里只不过定义的三维都是2,可以在前面的宏定义改成你需要的数字。如果需要比较大块的内存,我建议还是使用虚拟内存管理函数,像VirtualAlloc函数

解决方案 »

  1.   

    你需要知道:
    1. 多维数组在内存中的layout其实是Flat的
    2. 指向数组的指针工作原理.
    3. 元素offset计算是在编译期确定的,所以你所说的"三维数组的每一维长度可以自己定义"需要模板支持才能完成.如果这三点你都清楚的话,请参考下面这段代码.
    short * pBuf = new short[20 * 30 * 40];
    if (NULL == pBuf)
    {
    //...
    } short (*pDimPtr)[30[40] = (short (*)[30][40])pBuf;
    pDimPtr[1][2][3] = 0xEEEE;至于申请方法,只要你需要的空间不是太大,并且对性能要求不是很高,可以直接使用new, 否则可以自建堆来分配
      

  2.   

    不用那么麻烦:
    直接new就行了,别忘记不用时删除掉!
      

  3.   

    jasonshark(没暑假了...) 
     正解
      

  4.   

    unsigned char * buffer;
    int a=2;b=3;c=4;buffer = (unsigned char *)malloc(a*b*c);
    /* the buffer is you want */
    /* remember: you should free the buffer when you didn't used */