Findios
Findios

Reputation: 307

differences in 3d array allocation in C++

I'm working with some images in c++ so, due to size, I need to allocate some arrays.

I need to use an specific function that only take array as argument.

If I initialize the array as a global variable like this: double buffer[193][229][193]; I can use the array buffer in the function with no problem.

However if I try to allocate a new array with this function:

    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;
}

The function stops working. I get no error messages, the programs just exits which means that exist some problem with the array index.

Basically, I want to know what is the difference between the function I am using and just declare the array as double buffer[193][229][193]. Thank everyone for the help.

-----------------------------------------EDIT - FULL CODE ------------------------------

 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;

Upvotes: 0

Views: 87

Answers (0)

Related Questions