0

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;
trincot
  • 317,000
  • 35
  • 244
  • 286
Findios
  • 307
  • 1
  • 4
  • 14
  • Did you try a debugger? Why does the program exit? At first (admittedly brief) glance, I don't see any problems here. – Carl Norum Jun 19 '14 at 18:07
  • In your function you are allocating 2d array of **pointers** to 1d double arrays, while `double buffer[193][229][193]` allocates 3d array of doubles. – JarkkoL Jun 19 '14 at 18:08
  • can i change the function to allocate doubles? – Findios Jun 19 '14 at 18:10
  • @Findios - your code looks fine. Can you show some more context of the use case you have? The problem is likely to be there. – Carl Norum Jun 19 '14 at 18:11
  • @Findios Yes: `Type *p=new Type[x*y*z];` – JarkkoL Jun 19 '14 at 18:11
  • I aded the full code. The problem is within miget_real_value_hyperslab function. Has I said, if i initialize as a global variable the function works, if i allocate the array it doesn't. – Findios Jun 19 '14 at 18:20
  • @JarkkoL I am trying to get an 3D array. – Findios Jun 19 '14 at 18:28
  • @Findios Allocate 1d array and calculate the 3d array index yourself. That's how it's done in C++ for dynamic multi-dimensional arrays: `p[x+y*193+z*193*229];` – JarkkoL Jun 19 '14 at 18:35
  • I'am trying to avoid that because I'm working with big images and using an processing algorithm that is already very heavy. If I have to calculate index it will increase the computation time tremendously – Findios Jun 19 '14 at 18:39
  • 1
    Compiler will do that for you anyway, so there's no performance difference. In fact you can optimize this yourself if you do it manually by omitting part of the calculation, e.g. when you process a row of pixels, just calculate `ri=y*193+z*193*229` for the row and just do `ri+x` for the pixels in the row – JarkkoL Jun 19 '14 at 18:40
  • @Findios your function is working, but `miget_real_value_hyperslab` seems to accept continuous 1D arrays embedding the 3D. as shown isn http://en.wikibooks.org/wiki/MINC/Tutorials/Programming05 – Micka Jun 19 '14 at 18:57

0 Answers0