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

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    4

    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:

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

    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!

    Thanks in advance!
    - 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];
            }
        }
    Attached Images Attached Images Splitting a 3D dynamically allocated array by iterative division into halves-splittingsteps-png 
    Last edited by MoGuL; 11-30-2011 at 05:25 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamically Allocated Multidimensional Array
    By LyTning94 in forum C++ Programming
    Replies: 19
    Last Post: 08-09-2011, 07:13 AM
  2. Dynamically Allocated Array
    By chris4 in forum C Programming
    Replies: 9
    Last Post: 05-06-2011, 10:01 AM
  3. Initializing a 2D array dynamically allocated
    By newboyc in forum C Programming
    Replies: 5
    Last Post: 02-01-2011, 01:08 PM
  4. Dynamically allocated array
    By dre in forum C Programming
    Replies: 17
    Last Post: 08-13-2009, 06:57 PM
  5. Dynamically Allocated Array
    By vb.bajpai in forum C Programming
    Replies: 3
    Last Post: 06-17-2007, 08:40 AM