Results 1 to 3 of 3

Thread: pointer arithmetics

  1. #1
    Join Date
    Jan 2007
    Posts
    68
    Thanks
    9
    Thanked 8 Times in 8 Posts

    Question pointer arithmetics

    Hello,

    I don't want to start a "flamewar" on how to create a more efficient x-dim-array in c++,
    but I'd like to know how to get the 3d equivalent of the following 2d code:
    Qt Code:
    1. int main()
    2. {
    3. int height, width, depth;
    4. height = width = depth = 3;
    5.  
    6. //---- 2d case ----
    7. int **pMat2D = NULL;
    8. pMat2D = new int *[height];
    9. pMat2D[0] = new int [height * width];
    10. for (int i = 0; i < height; i++)
    11. pMat2D[i] = &pMat2D[0][(i*width)];
    12.  
    13. //fill
    14. int number = 0;
    15. for(int i = 0; i < height; i++)
    16. for (int j = 0; j < width; j++)
    17. pMat2D[i][j] = ++number;
    18.  
    19. //print
    20. for(int i = 0; i < height; i++){
    21. for (int j = 0; j < width; j++){
    22. std::cout << pMat2D[i][j] << " ";
    23. }
    24. std::cout << std::endl;
    25. }
    26.  
    27. //clean
    28. delete pMat2D[0];
    29. delete[] pMat2D;
    30. //---- 2d case end ----
    31.  
    32. //---- 3d case ----
    33. int ***pMat3D = NULL;
    34. //???
    35. //---- 3d case end ----
    36. return 0;
    37. }
    To copy to clipboard, switch view to plain text mode 

    advantages of the 2d version above:
    array access via [][];
    data populates one continuous chunk of memory;
    fast access on random [i][j]s
    no need to calculate offsets during access, as in:
    Qt Code:
    1. //2d case
    2. int *pMat = new int[x*y];
    3. //access via
    4. pMat[(x * y_size + y)];
    5.  
    6. //3d case
    7. int *pMat = new int[x*y*z];
    8. //access via
    9. pMat[((z * x_size + x) * y_size + y)];
    To copy to clipboard, switch view to plain text mode 

    i need the array to be especially fast with random access on its data.
    in the 2d case (in my app) the code from the top performs much better than the code from above, where you access the data via arr[(x * y_size + y)]

    i'd like to have a 3d version of the 2d version @ the top, but somehow I can't figure it out.
    everything I try, results in some segmentation faults :/...

    regards
    darksaga
    Last edited by darksaga; 25th April 2008 at 00:00.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: pointer arithmetics

    Qt Code:
    1. int * data = new int[ width * height * depth ];
    2. int ** lut1 = new int *[width];
    3. int *** lut2 = new int **[width * height ];
    4.  
    5. for( int i = 0; i < width; ++i ) {
    6. lut1[i] = & lut2[width*i];
    7. for( int j = 0; j < height; ++j ) {
    8. lut1[i][j] = & data[height*(width*i + j)];
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 
    (not tested of course )

  3. #3
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: pointer arithmetics

    The, ehm, preferred way is to create a class that overloads the () operator, which can take any number of parameters. Separate subscripts should be used for jagged arrays, which this is not. It has the added advantage of making the allocation a lot easier.

    Just a suggestion.
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

Similar Threads

  1. Delete auto pointer from QMultiMap
    By phillip_Qt in forum Qt Programming
    Replies: 5
    Last Post: 3rd December 2007, 17:29
  2. Passing a pointer in Signal/Slot Connection
    By mclark in forum Qt Programming
    Replies: 4
    Last Post: 6th November 2007, 19:04
  3. taking pointer out of bounds nicely
    By baray98 in forum General Programming
    Replies: 4
    Last Post: 21st October 2007, 00:33
  4. Pointer to ActiveX object in QT
    By json84 in forum Qt Programming
    Replies: 1
    Last Post: 15th June 2007, 12:42
  5. QDataStream, QTreeWidgetItem, pointer
    By baca in forum Qt Programming
    Replies: 6
    Last Post: 16th February 2007, 16:59

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.