Monday, December 8, 2008

How One-dimensional Array is Stored in Memory

You might have noticed that in C, the rightmost subscript of a two-dimensional array varies faster than the leftmost (in fact, there are no multidimensional arrays in C, but array of arrays). This fact suggests that the array is stored in a row major addressing format. However, for a one-dimensional array the case is the simplest: The array occupies a contiguous block of memory. The array -

int a[10];

- is laid out in memory as a contiguous block, as shown below:

One dimensional array storage pattern


Elements of array are stored in the successive increasing locations of memory. For example, if the array starts at memory location 0×1000, then with our assumed size of an integer (4 bytes), the first element is stored at location 0×1000, the second element at location 0×1004, and so on.

0 comments:

Post a Comment