Problem with multidimensional vector

My problem is this: in the main function declare a vector cubic in three dimensions.
 
int cubic[a][b][c];


After processing user input, I have to pass the vector to other functions that operate on this array, but I don't know how to declare the functions.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void roto(int cubic[][][], int cube, int row, bool direction, int nrot)
{
    int tmp[cube];
    int i;

    while(nrot-- > 0)
    {
    for(i = 0; i <= cube - 2; i++)
    {
      memcpy(tmp, cubic[i + 1][row], 10);
      memcpy(cubic[i + 1][row], cubic[0][row], 10);
      memcpy(cubic[0][row], tmp, 10);
    }
    }
}


Should I allocate dynamically the carrier?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int ***cubic = malloc(sizeof(int) * 1000);

void roto(int*** cubic, int cube, int row, bool direction, int nrot)
{
    int tmp[cube];
    int i;

    for(i = 0; i <= cube - 2; i++)
    {
      memcpy(tmp, cubic[i + 1][row][0], sizeof(int) * cube);
      memcpy(cubic[i + 1][row][0], cubic[0][row][0], sizeof(int) * cube);
      memcpy(cubic[0][row][0], tmp, sizeof(int) * cube);
    }
}


It doesn't work.
Pointers to pointers are not the same as "straight" multidimentional arrays. They operate very, very differently under the hood and cannot be used interchangably.

You are creating a 1D array (int*) and casting it to a 3D array (int***) which will fail horribly because the compiler will be treating the ints in the array as if they were pointers, and dereferencing memory which does not exist.

Multidimentional dynamic memory is a pain, which is one of the reasons why I avoid it. If you want to do it you'll need to do one of 2 things:

1) Use a 1D array and simulate the other 2 dimensions with calculations:

1
2
3
4
5
6
7
int* array = malloc(sizeof(int) * width * height * depth);

// to get x,y,z:
array[ ((z * height) + y) * width + x ] = 5;

// cleanup
free(array);



or 2) allocate each dimension separately (use the pointer to pointer to pointer approach):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int*** array = malloc(sizeof(int**) * depth);
for(int i = 0; i < depth; ++i)
{
  array[i] = malloc(sizeof(int*) * height);
  for(int j = 0; j < height; ++j)
    array[i][j] = malloc(sizeof(int) * width);
}

// to get x,y,z
array[z][y][x] = 5;

// cleanup:
for(int i = 0; i < depth; ++i)
{
  for(int j = 0; j < height; ++j)
    free(array[i][j]);
  free(array[i]);
}
free(array);



As you can see, both approaches are very ugly.

In C++ either approach can be wrapped in a class to make it less error prone and easier to use. Since you seem to be using C, you can do the same thing with structs.
Thanks, I've understood how to dynamically allocate memory for a multi-dimensional vector, but I can't understand how a structure can replace a multi-dimensional array.
It's object oriented programming. You make a struct which represents an array and then use that struct and its support functions rather than the array directly:

1
2
3
4
5
6
7
8
9
typedef struct
{
  //...  multidimentional stuff here
} array3d;

array3d* array3d_create(int width, int height, int depth) { /*...*/ }
void array3d_destroy(array3d* ar) { /*...*/ }
int array3d_get(array3d* ar, int x, int y, int z) { /*...*/ }
void array3d_set(array3d* ar, int x, int y, int z, int val) { /*...*/ }

1
2
3
4
5
6
7
8
// then in your main code, instead of using an array directly...
array3d* myarray = array3d_create( 10, 10, 10 );  // make a 10x10x10 array

int val = 6;
array3d_set( myarray, 1, 2, 3, val ); // instead of myarray[1][2][3] = val
val = array3d_get( myarray, 1, 2, 3 );  // instead of val = myarray[1][2][3]

array3d_destroy( myarray ); // to clean up 


That way it's easier to use. You can also easily pass array3d*'s between functions.


Of course C++ makes it easier with classes and operator overloading... but it can still be done in C. It's just a little more work and a bit uglier.
Last edited on
Why after allocating dynamically the vector three-dimensional cubic, I can access it, for example, to print a value that contains (printf ("% s", cubic [num] [num] [num])), but can not Please use memcpy:

 
memcpy (cubic, mio_array, sizeof (mio_array));


mio_array is a one-dimensional arrays of the same size as cubic.
Last edited on
Topic archived. No new replies allowed.