简体   繁体   English

2D数组指针算法

[英]2D array pointer arithmetic

Say I have an int array: int arr[5][5] and assume C language memory management. 假设我有一个int数组: int arr[5][5]并假定使用C语言内存管理。

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] 假设我想访问以下元素: 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 . 我打印了arrarr+1arr+2以及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). 我想知道为什么每个地址之间的差异是14(十六进制)或20(十进制)。

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. 在堆栈上分配一个指针arr
  2. The arr pointer holds the address to the starting location of an array of pointers. arr指针将地址保存到指针数组的起始位置。
  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. 编辑:另外,假设您想通过指针算法访问arr [i] [j]。 How would this be done? 怎么做? If the array is allocated dynamically, I think you can do *( * (arr+i)+j). 如果数组是动态分配的,我想你可以做*(*(arr + i)+ j)。 I'm not sure how you do this for static allocations? 我不确定您如何针对静态分配执行此操作? My guess is *(arr + ((row_size * (i))+j)). 我的猜测是*(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). 我想知道为什么每个地址之间的差异是14(十六进制)或20(十进制)。

Perhaps because on your system the size of int is 4 and there are 5 ints in the inner array. 可能是因为在您的系统上int的大小为4 ,而内部数组中有5 int。

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] ): C++标准中有一个注释(在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. [注意:由此可见,C ++中的数组是按行存储的(最后一个下标变化最快),并且声明中的第一个下标有助于确定数组消耗的存储量,但在下标计算中不起作用。 —end note ] —尾注]

The linear address of location [i,j] in a 2d matrix is calculated by: 通过以下公式计算二维矩阵中位置[i,j]的线性地址:

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]. (1)arr [5] [5]的所有5 * 5个元素的内存地址为&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). 它们都按sizeof(int)的不同顺序排列。 In your os, sizeof (int) = 4. 在您的操作系统中,sizeof(int)= 4。

(2) From the perspective of address, arr+1 = &arr[1][0], arr+2 = &arr[2][0]. (2)从地址的角度来看,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. 假设起始存储位置为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. 每个数组都有5个整数, sizeof(int)为4。因此,对于5个整数,它将分配20个字节的内存。 Due to this only you are getting the difference between each array address is 20 bytes(in decimal. In hexadecimal it is 14) 因此,每个数组地址之间的差是20个字节(十进制。十六进制为14)。


1. Allocate a pointer `arr` on stack.

-No. -没有。 Allocate a pointer to pointer to a integer ( arr ) on stack. 在堆栈上分配一个指向整数( arr )的指针。 That is int **arr; 那是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! 但这是动态内存分配,不是静态的!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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