简体   繁体   English

为什么分配的二维int数组的存储地址序列与普通的数组不同?

[英]Why does an allocated 2-dimensional int array have a different sequence of memory addresses as a normal one?

The following code outputs the sequence of memory addresses of a dynamically allocated array, then a regular 2D array (of ints) via nested for loops. 以下代码输出动态分配的数组的内存地址序列,然后通过嵌套的for循环输出常规的2D数组(整数)。

#include <iostream>
using namespace std;

int main()
{
    int regArray[3][3]; //a normal 2d array
    int **allocatedArray = new int*[3];

    for (int i = 0; i < 3; i++)
        allocatedArray[i] = new int[3]; //an allocated 2d array


    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            cout << &allocatedArray[i][j] << "    " << &regArray[i][j] << endl; 
            //prints the allocated array, leaves a space, then a regular one
}

The output is as follows: 输出如下:

0x7812e8    0x29febc
0x7812ec    0x29fec0
0x7812f0    0x29fec4
0x781300    0x29fec8
0x781304    0x29fecc
0x781308    0x29fed0
0x781318    0x29fed4
0x78131c    0x29fed8
0x781320    0x29fedc

Process returned 0 (0x0)   execution time : 0.249 s
Press any key to continue.

I know that in the regular array (right side), the next element's address results in an increase of 4 bytes (in hexadecimal form). 我知道在常规数组(右侧)中,下一个元素的地址导致增加4个字节(以十六进制形式)。 However, in the allocated array (left side) this does not seem to be the case. 但是,在分配的数组(左侧)中似乎并非如此。 During the execution of the inner for loop, there is a normal increase of 4 bytes, as expected. 在执行内部for循环期间,通常会增加4个字节,这与预期的一样。 Whenever the outer loop iterates, there seems to be an increase of 10. 每当外循环迭代时,似乎都会增加10。

An example is when: 0x7812f0 jumps to 0x781300. 例如:0x7812f0跳转到0x781300。

Why does this occur? 为什么会发生这种情况? Any simple explanation is appreciated. 任何简单的解释都值得赞赏。

Both of your arrays are allocated. 您的两个数组均已分配。 But they use different forms of allocation ( automatic and dynamic respectively). 但是它们使用不同的分配形式(分别是自动分配和动态分配)。

Your first array places 9 ints in 9 consecutive memory locations. 您的第一个数组在9个连续的内存位置中放置9个整数。

Your second array allocates three separate blocks of 3 consecutive memory locations. 您的第二个数组分配3个连续的内存位置的三个单独的块。 Those 3 blocks could be anywhere, they do not have to be next to each other in memory. 这三个块可以在任何地方,它们在内存中不必彼此相邻。

Your output confirms this. 您的输出确认了这一点。

The code below means to allocate a pointer to a pointer array 下面的代码意味着将指针分配给指针数组

int **allocatedArray = new int*[3];

and here is to make each of the pointer a allocated array 这是使每个指针分配一个数组

allocatedArray[i] = new int[3];

The code above allocates a int array which should be 12 bytes, but between each 2 of the 1-dimensional array have no relationship. 上面的代码分配了一个int数组,该数组应该为12个字节,但是一维数组的每2个之间都没有关系。

So, the size of allocatedArray[i][j] is 4 bytes and allocatedArray[i] is 12 bytes 因此, allocatedArray[i][j]的大小为4个字节,而allocatedArray[i]为12个字节

the 10 bytes you mentioned is just a coincidence 您提到的10个字节只是一个巧合

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

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