C ++多维数组在幕后的工作方式 [英] C++ How multi-dimensional arrays works behind the scenes

查看:102
本文介绍了C ++多维数组在幕后的工作方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C ++的新手,试图理解一件事.我听说所有数组都只是指针别名,并且[]运算符执行的每个操作都将转换为指针取消引用.请参见下面的示例(动态数组).

I am newbie in C++ and trying to understand one important thing. I have heard that all arrays are only pointer aliases and every action done by [] operator is converted to pointer dereference. See the example below (dynamic array).

auto **tab = new int*[2];
tab[0] = new int[2];
tab[1] = new int[2];

tab[0][0] = 1;
tab[0][1] = 2;
tab[1][0] = 3;
tab[1][1] = 4;

cout << "tab[0] address: " << &tab[0] << " with value: " << tab[0] << endl;
cout << "tab[1] address: " << &tab[1] << " with value: " << tab[1] << endl;
cout << "tab[0][0] address: " << &tab[0][0] << " with value: " << tab[0][0] << endl;
cout << "tab[0][1] address: " << &tab[0][1] << " with value: " << tab[0][1] << endl;
cout << "tab[1][0] address: " << &tab[1][0] << " with value: " << tab[1][0] << endl;
cout << "tab[1][1] address: " << &tab[1][1] << " with value: " << tab[1][1] << endl;

delete[] tab[0];
delete[] tab[1];
delete[] tab;

这是一个非常简单的程序,它显示了如何在内存中表示指针的指针.它返回以下结果:

This is pretty simple program, which shows how pointer to pointer is represented in memory. It returns the following result:

tab[0] address: 0x7114d0 with value: 0x7114e0
tab[1] address: 0x7114d4 with value: 0x7114f0
tab[0][0] address: 0x7114e0 with value: 1
tab[0][1] address: 0x7114e4 with value: 2
tab[1][0] address: 0x7114f0 with value: 3
tab[1][1] address: 0x7114f4 with value: 4

注意:我完全理解它的工作原理.我有两个基本地址,它们指向其他第二个地址.结果,取消引用tab[0]会将内存地址返回到第二维.伪代码:

Note: I fully understand how it works. I have two basic adresses, which points to the other second addresses. As a result dereferencing tab[0] returns memory address to the second dimension. Pseudocode:

*(0x7114d0) = 0x7114e0 -> 1

但是第二个示例显示了静态数组的工作原理.

But the second example shows how static arrays works.

int tab[2][2] = {{1,2}, {3,4}};

cout << "tab[0] address: " << &tab[0] << " with value: " << tab[0] << endl;
cout << "tab[1] address: " << &tab[1] << " with value: " << tab[1] << endl;
cout << "tab[0][0] address: " << &tab[0][0] << " with value: " << tab[0][0] << endl;
cout << "tab[0][1] address: " << &tab[0][1] << " with value: " << tab[0][1] << endl;
cout << "tab[1][0] address: " << &tab[1][0] << " with value: " << tab[1][0] << endl;
cout << "tab[1][1] address: " << &tab[1][1] << " with value: " << tab[1][1] << endl;

与上一个示例相同,我得到结果:

Same as in the previous example I get results:

tab[0] address: 0x28fea0 with value: 0x28fea0
tab[1] address: 0x28fea8 with value: 0x28fea8
tab[0][0] address: 0x28fea0 with value: 1
tab[0][1] address: 0x28fea4 with value: 2
tab[1][0] address: 0x28fea8 with value: 3
tab[1][1] address: 0x28feac with value: 4

在这一点上,我有一个很大的问题,如何理解这一点,因为取消引用'tab [0]'返回的地址'tab [0]'也包含value(1)...伪代码

And in this point I have a huge problem how to understand this, bacause dereferencing 'tab[0]' returns the address of 'tab[0]' which also hold value(1)... Pseudocode

*(0x28fea0) = 0x28fea0 -> 1

现在您如何理解它?似乎在内存中,静态和动态多维数组以两种不同的方式处理.如果数组实际上是指针,则tab[0]会处理自己的地址(下面的伪代码)……

And now how tu understand it? It seems that in memory static and dynamic multidimensional arrays are handle in two different ways. If arrays are in fact pointers how static arrays dereferencing works by the fact that tab[0] handle own address (pseudocode below)...

推荐答案

2D数组和指针数组是不同的动物,即使它们的语法相同.

2D arrays and arrays of pointers are different animals, even if their syntax is the same.

这是指向数组的指针的数组:

This is an array of pointers to arrays:

auto **tab = new int*[2];
tab[0] = new int[2];
tab[1] = new int[2];

tab是一个指针数组,tab的每个元素都指向一个 row ,但是这些行是彼此独立分配的,并且(不必)是连续的.从理论上讲,它们甚至可以具有不同的大小,这对于C字符串数组很常见.

tab is an array of pointers, each element of tab points to a row, but the rows are allocated independently of each others and are not (necessarily) contiguous. In theory they could even have different sizes, which is common for arrays of C-strings.

这是2D数组:

int tab[2][2];

它是一个数组数组.在这里,行必须具有相同的大小并连续分配:&tab[0][2]必须与&tab[1][0]

It is an array of arrays. Here the rows are required to have same size and are contiguously allocated: &tab[0][2] is required to be the same as &tab[1][0]

但是完全独立于静态或动态分配.这是一个静态的指针数组:

But is is completely independant of static or dynamic allocation. Here is a static array of pointers:

int row1[2] = { 0, 1};
int row2[2] = { 2, 3};
int *tab[] = { row1, row2 }; // row1 and row2 decay to pointers to their first element;

这是一个动态分配的2D数组:

And here is a dynamically allocated 2D array:

typedef int iarr2[2];
iarr2* dyntab = new iarr2[2];

这篇关于C ++多维数组在幕后的工作方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆