简体   繁体   中英

2D array pointer arithmetic

Say I have an int array: int arr[5][5] and assume C language memory management.

I want to access a particular element of the array using only pointer arithmetic and dereferencing.

Suppose I wanted to access the element in: arr[i][j]

I tried printing a few things at first to understand how the addresses worked. I printed the address of arr , arr+1 , and arr+2 , and arr+5 .

These were the results:

0x7fff58475b80
0x7fff58475b94
0x7fff58475ba8
0x7fff58475be4

I'm wondering why the difference between each of address is 14(in Hexadecimal) or 20 (in decimal).

Also, when you create a 2-D array on the stack, is a pointer to an array of pointers to memory blocks created? This is how I assume we allocate space for the array:

  1. Allocate a pointer arr on stack.
  2. The arr pointer holds the address to the starting location of an array of pointers.
  3. That array of pointers contains the starting location of a row in the 2-D array.

Is this correct?

EDIT: Also, assume you wanted to access arr[i][j] through pointer arithmetic. How would this be done? If the array is allocated dynamically, I think you can do *( * (arr+i)+j). I'm not sure how you do this for static allocations? My guess is *(arr + ((row_size * (i))+j)). Is this correct?

I'm wondering why the difference between each of address is 14(in Hexadecimal) or 20 (in decimal).

Perhaps because on your system the size of int is 4 and there are 5 ints in the inner array.

To get the idea about layout, try the following code:

int a[5][5];
int k = 0;
for (int i = 0; i < 5; i++)
    for (int j = 0; j < 5; j++)
        a[i][j] = ++k;
int* p = &a[0][0];
for (int n = 0; n < 25; n++, p++)
    printf("%d ", *p);

Output is

1 2 3 4 ... 25

There is a note in the C++ standard (in 8.3.4 Arrays [dcl.arrays] ):

[ Note: It follows from all this that arrays in C++ are stored row-wise (last subscript varies fastest) and that the first subscript in the declaration helps determine the amount of storage consumed by an array but plays no other part in subscript calculations. —end note ]

The linear address of location [i,j] in a 2d matrix is calculated by:

linear_position = i * (capacity of column 0) + j;

The address is calculated from the linear position by using the size of array type and using the starting address of the array:

cell_address = &array[0][0] + linear_position * sizeof(array[0][0]);

Plugging in your values into the above equations should give you an indication of the differences in addresses.

(1)The memory address of all 5*5 elements of arr[5][5] is &arr[0][0], &arr[0][1] ... & arr[0][4], &arr[1][0], ... &arr[1][4], ... &arr[4][4]. They are all arranged in sequence with the difference of sizeof (int). In your os, sizeof (int) = 4.

(2) From the perspective of address, arr+1 = &arr[1][0], arr+2 = &arr[2][0]. They are just different representation. The former is pointer form, the later is array form.

Array is a collection of similar data elements stored in contiguous memory location.

int arr[5][5];

In this declaration, it will allocate contiguous memory location for all elements. say starting memory location is 1000.

array  starting   array elements
       address
arr[0]--> 1000 --> arr[0][0] arr[0][1] ... arr[0][4]
arr[1]--> 1020 --> arr[1][0] arr[1][1] ... arr[1][4]
arr[2]--> 1040 --> arr[2][0] arr[2][1] ... arr[2][4]
arr[3]--> 1060 --> arr[3][0] arr[3][1] ... arr[3][4]
arr[4]--> 1080 --> arr[4][0] arr[4][1] ... arr[4][4]

I'm wondering why the difference between each of address is 14(in Hexadecimal) or 20 (in decimal).

Each array has 5 integer, sizeof(int) is 4. So for 5 integers it will allocate 20 bytes of memory. Due to this only you are getting the difference between each array address is 20 bytes(in decimal. In hexadecimal it is 14)


1. Allocate a pointer `arr` on stack.

-No. Allocate a pointer to pointer to a integer ( arr ) on stack. That is int **arr;

2. The arr pointer holds the address to the starting location of an array of pointers.
3. That array of pointers contains the starting location of a row in the 2-D array.

Now it is correct! But This is dynamic memory allocation not static!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM