0
float sampleGrid1[5][5][5] =
{
    {
        {0.0, 0.0, 0.0, 0.0, 0.0},
        {0.0, 0.0, 0.0, 0.0, 0.0},
        {0.0, 0.0, 0.0, 0.0, 0.0},
        {0.0, 0.0, 0.0, 0.0, 0.0},
        {0.0, 0.0, 0.0, 0.0, 0.0}

    },

    {
        {0.0, 0.0, 0.0, 0.0, 0.0},
        {0.0, 0.0, 0.0, 0.0, 0.0},
        {0.0, 0.0, 1.0, 0.0, 0.0},
        {0.0, 0.0, 0.0, 0.0, 0.0},
        {0.0, 0.0, 0.0, 0.0, 0.0}

    },

    {
        {0.0, 0.0, 0.0, 0.0, 0.0},
        {0.0, 0.0, 1.0, 0.0, 0.0},
        {0.0, 1.0, 1.0, 1.0, 0.0},
        {0.0, 0.0, 1.0, 0.0, 0.0},
        {0.0, 0.0, 0.0, 0.0, 0.0}

    },

    {
        {0.0, 0.0, 0.0, 0.0, 0.0},
        {0.0, 0.0, 0.0, 0.0, 0.0},
        {0.0, 0.0, 1.0, 0.0, 0.0},
        {0.0, 0.0, 0.0, 0.0, 0.0},
        {0.0, 0.0, 0.0, 0.0, 0.0}

    },

    {
        {0.0, 0.0, 0.0, 0.0, 0.0},
        {0.0, 0.0, 0.0, 0.0, 0.0},
        {0.0, 0.0, 0.0, 0.0, 0.0},
        {0.0, 0.0, 0.0, 0.0, 0.0},
        {0.0, 0.0, 0.0, 0.0, 0.0}

    }
};

typedef struct
{
  int Nx;
  int Ny;
  int Nz;
  float*** M;
}OG;

Relevant function:

OG *newOG(){
    OG *newOG = (OG *)malloc(sizeof(OG));
    if (newOG == NULL)
    {
        throw std::exception("newOG : no memory is available");
    }

    return newOG;
}

int initiateOG(OG *MyOG)
{

    ifstream dump("OGdump3.txt");

    if (dump.is_open())
    {   
        while ( dump.good() )
        {   
            dump >> MyOG->Nx;
            dump >> MyOG->Ny;
            dump >> MyOG->Nz;

            MyOG->M = new float**[MyOG->Nx];
            for(int i = 0; i < MyOG->Nx; i++)
            {
                MyOG->M[i] = new float*[MyOG->Ny];

                for(int j = 0; j < MyOG->Ny; j++)
                {
                    MyOG->M[i][j] = new float[MyOG->Nz];
                }
            }
            for(int z=0;z < MyOG->Nz; z++){

                for(int y=0;y < MyOG->Ny; y++){

                    for(int x=0;x < MyOG->Nx; x++){

                        dump >> MyOG->M[x][y][z];
                    }

                }

            }


        }

    dump.close();
    }
    else return 0; 
    return 1;
}

I want to hard code some sample grids into the code, but don't know the best way to create them, do i have to use for loops?

i don't want to change my typedef struct OG, if possible

Modified:

OG *occupancyGrid;

 void initialize3dArray(int x, int y, int z,float*** array)
    {
        array = new float**[x];
        for(int i = 0; i < x; i++)
        {
            array[i] = new float*[y];

            for(int j = 0; j < y; j++)
            {
                array[i][j] = new float[z];
            }
        }

    }

   void sampleOG1()
    {
        occupancyGrid = newOG();
        occupancyGrid->Nx = 5;
        occupancyGrid->Ny = 5;
        occupancyGrid->Nz = 5;

        initialize3dArray(5, 5, 5,occupancyGrid->M);

        for(int z=0;z < occupancyGrid->Nz; z++){

            for(int y=0;y < occupancyGrid->Ny; y++){

                for(int x=0;x < occupancyGrid->Nx; x++){

                    occupancyGrid->M[x][y][z] = sampleGrid1[x][y][z];
                }

            }

        }

    }

initialize3dArray this function doesn't have compiling error, but still causing the program to crash

3
  • rectest.cpp: In function ‘OG* sampleOG1()’: rectest.cpp:59: error: ‘newOG’ was not declared in this scope rectest.cpp:63: error: ‘occupancyGrid’ was not declared in this scope Commented Apr 3, 2011 at 14:55
  • I'm happy to that there is a related function (relevance?). No problem with the initial snippet here. What is your exact question? Commented Apr 3, 2011 at 14:55
  • @Oswald: reporting your own compilation failures is hardly helpful Commented Apr 3, 2011 at 14:55

3 Answers 3

3

Yes. that will not compile, because float[5][5][5] and float *** aren't same type. They're not even compatible type. One cannot convert to other automatically.

However, float[5][5][5] can convert to float (*)[5][5] automatically. So this is legal code:

    float (*m)[5][5];
    m = sampleGrid1;  //legal - allowed!

Demo : http://ideone.com/RwAwI

So define OG as,

struct OG
{
  int Nx;
  int Ny;
  int Nz;
  float (*M)[5][5];
};

If you OG as defined above, then you can write this:

OG* temp = newOG();
temp->Nx = 5;
temp->Ny = 5;
temp->Nz = 5;
occupancyGrid->M = sampleGrid1; //DONT use &
Sign up to request clarification or add additional context in comments.

Comments

1

Isn't the error clear? float[5][5][5] is not as related to float*** as you think it is.

Use a std::vector<std::vector<std::vector<float> > > instead and avoid the whole mess.

2 Comments

i’m really new to c++,only had experience in java and c,could u give an example of using std::vector<std::vector<std::vector<float> > >
@Miranda: I think that's beyond the scope of this site. Read the chapter in your C++ book about vectors.
1

float x[2][2] is a 2D array of float - it's not an 1D array of pointers to float. The conversion from array to pointer only works for the first dimension of such arrays.

Given float x[2][2] you reserve space for 4 floats. A float ** variable on the other hand is a pointer to a pointer to a float - there's no pointers anywhere in float x[2][2]

The same of course holds true for a 3D array - your 3D array has no sneakily hidden pointers inside it, it cannot be treated as a float ***

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.