Thread: Treating a 2D array as a 1D array....

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    119

    Treating a 2D array as a 1D array....

    Is it more efficient to treat a 2D array as a 1D array in C? If I had a 2D array, array[8][8] and I accessed it via a pointer arrayPtr++ for all 63 elements. As opposed to two for loops, doing array[i][j]. Or is there no difference at all? Is it better to do one or the other? Or are they both equal statements and it depends on what I feel like doing?

    Code:
    **NOTE: this is just example code to illustrate my meaning, please don't comment on the code, I did not write it with the intent to compile or run it.
    
    1)
    
    arrayPtr = &board[0][0];
    
    for( i=0; i<63; i++ )
    {
       char ch = *arrayPtr++;
    }
    
    2)
    
    for( i=0; i<8; i++ )
    {
       for(j=0; j<8; j++ )
      {
          char ch = board[i][j];
      }
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, that will do equivalent things.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    No, they will do two different things. Mostly because of:
    If I had a 2D array, array[8][8] and I accessed it via a pointer arrayPtr++ for all 63 elements.
    8 * 8 == 64, not 63. There are 64 elements, but the last usable index is 63. However, your two code snippets differ in this respect. The first will make 63 iterations, the latter 64.

    As to which is more efficient, it probably won't matter which you use. If you notice any problems, let a profiler tell you what's taking up processing time, and optimize there. Otherwise, I'd probably go with the second myself.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    119
    Cactus_Hugger: 0 - 63 is the same as 1 - 64.....

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by John_L View Post
    Cactus_Hugger: 0 - 63 is the same as 1 - 64.....
    Yes, _BUT_ Cactus_Hugger makes a good catch:
    Code:
    for( i=0; i<63; i++ )
    will stop at 62, not 63.
    Should be (i < 64) to be correct - sorry I missed that. Well spotted Cactus_hugger.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by John_L View Post
    Cactus_Hugger: 0 - 63 is the same as 1 - 64.....
    But there are 64 elements. The particular indexing isn't the point.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multiplying a 2D array by a 1D array
    By youngvito in forum C Programming
    Replies: 14
    Last Post: 06-12-2009, 03:50 PM
  2. two dimensional dynamic array?
    By ichijoji in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 04:27 PM
  3. how to pass 2D array into function..?
    By IngramGc in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2001, 08:41 AM
  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