Splitting a 3D dynamically allocated array by iterative division into halves

This is a discussion on Splitting a 3D dynamically allocated array by iterative division into halves within the C Programming forums, part of the General Programming Boards category; Hi guys, I'm trying to split a binary (only elements are 0 and 1) dynamically allocated 3D array into seperate ...

1. Splitting a 3D dynamically allocated array by iterative division into halves

Hi guys,

I'm trying to split a binary (only elements are 0 and 1) dynamically allocated 3D array into seperate and smaller 3D arrays. The following figure makes it a bit clearer to understand:

In order to obtain the result, I'm trying to start and allocate new 3D arrays with the right dimensions and filling them. Unfortunately I'm not able to get my dynamic allocation to work for the 'omega_e' array. I want to allocate an array with dimensions [256..511]x[0..511]x[0..85], but I don't know how to do that. Normally you allocate arrays starting with index 0, but I want to start at index 256 and then I want to have the rest of the elements stored in the following indices. A solution would be to allocate an array of 512 elements, but that's not really what I want to do...

Maybe there is another solution to my splitting problem? Any advice would be helpfull!

- MoGuL

Code:
```    int i,j,k;
int s = 2;
int p = 9;
int q = pow(2,s)-1;
int dimx = 512;
int dimy = 512;
int dimz = (pow(2,p)-1)/q;

mexPrintf("Unsplit:\n");
mexPrintf("I = [0 .. %i] x [0 .. %i] x [0 .. %i]\n",dimx-1,dimy-1,dimz-1);

int vx,vy,f;

// Construct the initial binary tridimensional matrix M
int ***m;

// Dynamically allocate needed memory
m =  new int**[dimx];
for(i=0; i<dimx; i++){
m[i] = new int*[dimy];
for(j=0; j<dimy; j++){
m[i][j] = new int[dimz];
for(k=0; k<dimz; k++) {
//Initialise all elements to zero
m[i][j][k]=0;
}
}
}

// Fill the tridimensional matrix M with 1's at specific places
for(i=0; i<nbVertices; i++) {
vx = vertices2[0][i];
vy = vertices2[1][i];
f = ceil(data2[i]/q); // Uniform quantization
m[vx][vy][f] = 1;
}

// Split level #1
// --------------

int dimx_w,dimy_w,dimz_w;
int dimx_e,dimy_e,dimz_e;
int tmp;

//OMEGA_W: i-axis divided, j-axis unchanged, k-axis unchanged
dimx_w = floor((dimx-1)/2.0);
dimy_w = dimy-1;
dimz_w = dimz-1;

//OMEGA_E: i-axis divided, j-axis unchanged, k-axis unchanged
dimx_e = ceil((dimx-1)/2.0);
dimy_e = dimy-1;
dimz_e = dimz-1;

//OMEGA_W: i-axis divided, j-axis unchanged, k-axis unchanged
mexPrintf("I_w = [0 .. %i] x [0 .. %i] x [0 .. %i]\n",dimx_w,dimy_w,dimz_w);

//OMEGA_E: i-axis divided, j-axis unchanged, k-axis unchanged
mexPrintf("I_e = [%i .. %i] x [0 .. %i] x [0 .. %i]\n",dimx_e,dimx-1,dimy_e,dimz_e);

//Initialise needed resources
int ***omega_w;
int ***omega_e;

omega_w =  new int**[dimx_w];
for(i=0; i<dimx_w; i++){
omega_w[i] = new int*[dimy_w];
for(j=0; j<dimy_w; j++){
omega_w[i][j] = new int[dimz_w];
for(k=0; k<dimz_w; k++){
//Initialise all elements to zero
omega_w[i][j][k]=0;
}
}
}

omega_e =  new int**[dimx-1-dimx_e];
for(i=dimx_e; i<(dimx-1); i++){
omega_e[i] = new int*[dimy_e];
for(j=0; j<dimy_e; j++){
omega_e[i][j] = new int[dimz_e];
for(k=0; k<dimz_e; k++){
//Initialise all elements to zero
omega_e[i][j][k]=0;
}

}
}

for(i=0; i<nbVertices; i++) {
vx = vertices2[0][i];
if(vx <= dimx_w) {
omega_w[vx][vy][f] = m[vx][vy][f];
}
else {
omega_e[vx][vy][f] = m[vx][vy][f];
}
}```

2. Well you could do this
Code:
```#include <iostream>

int main ( ) {
int *p = new int[10] - 100;
int i;
for ( i = 100 ; i < 110 ; i++ ) {
p[i] = i;
}
delete [] (p + 100);
return 0;
}```
but such code is extremely dubious - use with care (and don't tell anyone where you found it - ok).

Rather than reallocating it all, could you just arrange for a set of pointer to point to relevant strips of data in the source array?
Like so.
Code:
```#include <iostream>

int main ( ) {
int *p = new int[10];
int *q = p + 5;
int i;
for ( i = 5 ; i < 10 ; i++ ) {
q[i] = i;
}
delete [] p;
return 0;
}```