2d array addressing

This is a discussion on 2d array addressing within the C++ Programming forums, part of the General Programming Boards category; Is a 2d array always stored contiguously? Will code like the following always give the same results? Code: int matrix[3][3]; ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    595

    2d array addressing

    Is a 2d array always stored contiguously? Will code like the following always give the same results?

    Code:
    	int matrix[3][3];
    	for( int i = 0, k = 0; i < 3; ++i ) {
    		for( int j = 0; j < 3; ++j, ++k ) {
    			matrix[i][j] = k;
    		}
    	}
    	for( int i = 0; i < 9; ++i ) {
    		std::cout << *(matrix[0]+i) << std::endl;
    	}

  2. #2
    Registered User Kernel Sanders's Avatar
    Join Date
    Aug 2008
    Posts
    61
    Yes. An array is a contiguous block of memory. In the case of this 2D array, it is a contiguous block of three arrays, which are contiguous blocks of three ints

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    The catch here is always, particularly if one means "guaranteed."

    Arithmetically, it appears good from an eyeball test.

    However, matrix[0] is an array of 3 ints, so any access beyond the third element is technically undefined. That is, as soon as i >= 3 is true. Computing the address one past the array is guaranteed, but not accessing it like the code is doing.

    There are environments that use array bounds checking (yes, even in C and C++) which could raise an exception to an out-of-bounds access like I described above. Whether that is true in reality or not, or whether that should be a real concern of yours, is arguable.

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Quote Originally Posted by whoie View Post
    There are environments that use array bounds checking (yes, even in C and C++) which could raise an exception to an out-of-bounds access like I described above.
    Well, yes, I mean "guaranteed". Is it guaranteed that the memory for the entire 2d array is contiguous?

    By "there are environments ..." were you referring to some specific compiler, or something else?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    It is guaranteed that the memory for the entire 2d array is contiguous.

    I would be surprised if array-bound checking could find anything wrong with this, since (a) the only index used on the array is 0 and (b) the dereferenced memory addresses lie within the array itself. (Note that the size-pointed-to is what is added; matrix[0] is of type int[3], so it decays to a pointer to int, which means that adding to it adds one int-sized piece at a time.)
    Last edited by tabstop; 08-18-2008 at 08:58 PM. Reason: subject/verb number agreement

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    OK, thanks.

  7. #7
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    Quote Originally Posted by tabstop View Post
    I would be surprised if array-bound checking could find anything wrong with this, since (a) the only index used on the array is 0 and (b) the dereferenced memory addresses lie within the array itself. (Note that the size-pointed-to is what is added; matrix[0] is of type int[3], so it decays to a pointer to int, which means that adding to it adds one int-sized piece at a time.)
    I tend to agree with you, and would share your surprise. Certainly nothing posted in that example is something I would consider to be "dancing on glass." Indeed, if we rewrote the expression to be *(&matrix[0][0]+i), I think it would greatly resemble a considerable amount of code from matrix libraries.

    However, I do believe it could be argued that it is technically undefined. I say that, because I am not convinced that an (admittedly) heavyweight bounds-checker wouldn't see matrix[0] and consider the underlying type to be "array of 3 int," (which it is). I doubt whether it would actually occur in practice, because those who put considerable value on bounds checking moved far away from C and C++ a long time ago. I simply wanted to point out something that could possibly be undefined, and wouldn't always give the same results. I'm not convinced either way at this point, and it wouldn't stop me from doing that if I had a good reason.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 04:45 AM
  2. 2D array becoming "deallocaded"
    By Aaron M in forum C Programming
    Replies: 2
    Last Post: 09-23-2006, 08:53 AM
  3. 2D array pointer?
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 09:16 AM
  4. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 01:39 PM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21