Thread: get nxn size blocks from 2d array?

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    4

    get nxn size blocks from 2d array?

    Hi all I am so confuse about getting nxn blocks from 2d array, for an instance:

    an 2d array of size is 512*512, and has numbers filled already and its like:

    array1[512][512] is

    Code:
    0,    1,    2, .........511
    512,  513'  514.......1023
    1024, 1025' 1026......1335
    	:
    and I'd like to copy 4x4 size block from array to
    array2[16384][4][4]
    (512*512/16=16384 is number of blocks)

    so 1st block of array2 will be

    Code:
    0,    1,    2,    3
    512,  513'  514,  515
    1024, 1025' 1026, 1027
    1336, 1337, 1338,  1339
    my code:
    Code:
    int k=0;
    for(i=0; i<512; i++)
    	for(j=0; j<512; j++)
    		  for(m=0; m<4; m++)
    			  for(n=0; n<4; n++){
    				array2[k][m][n]=array1[i%4][j%4];
    				k++;
    			  }
    but i got bus error and so confuse
    can anyone help me out please?
    Last edited by acfror; 10-28-2005 at 12:38 AM.

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    From my calculations, you are incrementing k way more than 31 times, but the first element of array2 only goes upto 31.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    4
    sorry k should up to 16384 b/c it is numbers of block with 4x4 size

  4. #4
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    But you have four nested for loops, with 512,512,4,4 iterations respectively, so you are incremending k 4*4*512*512 times = 4194304. So once you hit 16384, you're going out of bounds. re-think the logic of how you're doing the copy.

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Well that 3D array is ugly.

    Try doing this in 1D. First you will have 2 offsets. One offset indexes into the source array and one indexes into the destination array.

    Code:
    ...
    //start_row, end_row,start_column,end_column are parameters to this function
    int source_offset=((start_row)*source_width)+start_column;
    int source_start_offset=source_offset;
    int source_height_counter=0;
    int source_width_counter=0;
    int dest_offset=0;
    int source_width=end_column-start_column;
    int source_height=end_row-start_row;
    
    //Create destination array
    dest = new int[source_width*source_height];
    if (!dest) return;
    
    
    //Clear destination array
    memset(dest,0,source_width*source_height*sizeof(int));
    
    //Set loop flag
    bool bExitLoop=false;
    
    do
    {
      dest[dest_offset]=source[source_offset];
      source_offset++;
      dest_offset++;
      source_width_counter++;
      if (source_width_counter>source_width)
      {
         source_start_offset+=source_width;
         source_offset=source_start_offset;
         source_height_counter++;
         source_width_counter=0;
      }
      if (source_height_counter==source_height && source_width_counter==source_width)
      {
        bExitLoop=true;
      }
    } while (bExitLoop==false);
    Assuming my variables and code are correct, this should copy a NxN block from source to dest providing the following are true:
    • start_row, end_row, start_column, and end_column are all within the bounds of source array - in other words the starting row and column and the ending row and column of the requested source block do in fact lie within the boundaries of the array. No bounds checking is done in this code.
    • There is enough memory for the dest array
    • end_row is larger than start_row
    • end_column is larger than start_column


    I leave it as an exercise for you to put this into a function and return a pointer to dest array. This is not pure C - use malloc instead of new. Sorry, I'm so used to C++ I mix C and C++ all the time.

    In C/C++ this would be a good function to put into a template class because you could copy a block of NxN anything into dest. Dest would have the same data type as source and could easily be coded as a template class.
    Last edited by VirtualAce; 10-28-2005 at 03:23 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  3. Trouble with DMA Segmentation Faults
    By firestorm717 in forum C Programming
    Replies: 2
    Last Post: 05-07-2006, 09:20 PM
  4. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM
  5. 2d Array access by other classes
    By deaths_seraphim in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2001, 08:05 AM