C ++中3d数组分配的差异

时间:2014-06-19 17:59:07

标签: c++ arrays malloc heap alloc

我正在处理c ++中的一些图像,因此,由于尺寸的原因,我需要分配一些数组。

我需要使用一个只接受数组作为参数的特定函数。

如果我将数组初始化为像这样的全局变量:double buffer [193] [229] [193]; 我可以在函数中使用数组缓冲区,没有问题。

但是,如果我尝试使用此功能分配新数组:

    int Create3D_Array(Type ****pResult, int x, int y, int z)
{
    Type ***p = new Type **[x];
    for(int i = 0; i < x; i++)
    {
        p[i] = new Type *[y];
        for(int j = 0; j < y; j++)
            p[i][j] = new Type[z];
    }
    *pResult = p;
    return x * y * z;
}

该功能停止工作。我没有得到任何错误消息,程序只是退出,这意味着数组索引存在一些问题。

基本上,我想知道我正在使用的函数之间的区别是什么,只是将数组声明为双缓冲区[193] [229] [193]。 感谢大家的帮助。

-----------------------------------------编辑 - 完整代码 - ----------------------------

 int main() {
    const int yy =229;
    const int zz =193;
    const int xx =193;

    double ***test = NULL;
    Create3D_Array(&test, xx, yy, zz);


      /* open the volume - first and only command line argument */

    mihandle_t    minc_volume;
        int           result;
        /* open the volume - first and only command line argument */
        result = miopen_volume("image_name", MI2_OPEN_READ, &minc_volume);
        /* check for error on opening */
        if (result != MI_NOERROR) {
          fprintf(stderr, "Error opening input file: %d.\n", result);
        }

        unsigned long start[3], count[3];


          start[0] = start[1] = start[2] = start[3] = 0;
          count[0] = 193;
          count[1] = 229;
          count[2] = 193;
      std::cout<<test[1][0][0]<<std::endl;

 miget_real_value_hyperslab(minc_volume, MI_TYPE_DOUBLE, start, count, test));

        std::cout<<test[1][0][0]<<std::endl;
        Delete3D_Array(&test, xx, yy, zz);
        return 0;

0 个答案:

没有答案