简体   繁体   English

指向二维数组和分配的指针

[英]c++ - Pointer to 2d Array and allocation

reading this question: What is the difference between int* x[n][m] and int (*x) [n][m]? 阅读此问题: int * x [n] [m]和int(* x)[n] [m]有什么区别?

the correct reply stated this: 正确的回答是这样的:

int arr[2][3];
int (*p1)[2][3] = &arr;  // arr decays to int(*)[3], so we need to take the address
int (*p2)[2][3] = new int[42][2][3]; // allocate 42 arrays dynamically

the comment in that last line says it allocates 42 new arrays. 最后一行中的注释说它将分配42个新数组。 how does that work? 这是如何运作的? can someone explain that to me? 有人可以向我解释吗?

thanks! 谢谢!

i would reply on that question, but i can't, so new question it is (: 我会回答该问题,但我不能,所以新问题是(:

new int[3] allocates 3 integers. new int[3]分配3个整数。 Now new int[2][3] allocates 2 arrays, each of length 3 integers. 现在, new int[2][3]分配2个数组,每个数组的长度为3个整数。 Extending this further, new int[42][2][2] allocates 42 arrays, each of length 2 arrays, each of which in turn is, of length 2 integers. 进一步扩展这一点, new int[42][2][2]分配42个数组,每个数组的长度为2个数组,每个数组的长度为2个整数。 Declarations are really a recursive idea. 声明实际上是一个递归的想法。

Allright, class time. 好,上课时间。

C++ Multidementional arrays vs. Jagged array C ++多维度数组与锯齿数组

A multidemenional array (in C++) is a block of contiguous memory. 多维数组 (在C ++中)是一块连续的内存。 It allocates the full number of elements, all in a line, and then access them by combining the index accessors. 它在一行中分配全部元素,然后通过组合索引访问器来访问它们。 In essense. 本质上。 If I have an array defined by int x[2][3] , it essentially turns x[i][j] into (&x[0][0]+i*3)[j] . 如果我有一个由int x[2][3]定义的数组,则它实际上将x[i][j]变成(&x[0][0]+i*3)[j]

sizoef(int[2][3])==24 (4bytes/int*2*3) sizoef(int [2] [3])== 24(4bytes / int * 2 * 3)

This type of array is often called a "static" array, because the size of the array must be allocated at compile time. 这种类型的数组通常称为“静态”数组,因为必须在编译时分配数组的大小。

Note that the first size is irrelevent to this lookup. 请注意,第一个大小与该查询无关。 This makes it so that, when REFERENCING this type of array, we can exclude the smallest size from the type. 这样,当引用此类型的数组时,我们可以从该类型中排除最小的大小。 This makes is so that both the functions below are valid and can work on the array x declared above: 这使得以下两个函数均有效,并且可以在上面声明的数组x上工作:

int foo(int y[2][3]){ return y[1][1];}
int bar(int y[][3]){ return y[1][1];}

Note in this context, sizeof(y)==sizeof(void*), but that is a different problem all together. 注意在这种情况下,sizeof(y)== sizeof(void *),但这是一个完全不同的问题。

The typeing convention for static arrays behaves differently than your used to. 静态数组的键入约定的行为与您以前的行为不同。 Part of the type information comes AFTER the variable declaration. 类型信息的一部分来自变量声明之后。 This actually persists in typedefs as well: 实际上,这也持续存在于typedef中:

typedef int a[4][20];
typedef int b[][20];

If you wanted to take the address of such a value type, then what you need to declare the pointer to this array type. 如果要获取这种值类型的地址,则需要声明指向此数组类型的指针。 That can be done with: 可以通过以下方式完成:

int (*xptr)[2][3] = &x;

The int (*var)[...] says that var is a pointer to a int[2][3]. int (*var)[...]表示var是指向int [2] [3]的指针。

When people say C++ arrays are pointers, they are NOT refering to this type of array: the array is a little more compilicated than a pointer here, though we could flatten it into a 1D array. 当人们说C ++数组是指针时,它们并不是在指这种类型的数组:尽管我们可以将其展平为一维数组,但该数组比这里的指针要编译的多。

A jagged array (or dynamic array) is a single block of contiguose memory that is allocated in a linear fashion. 锯齿状数组 (或动态数组)是连续存储的单个块,以线性方式分配。 This type of array is often called dynamic because the size does NOT need to be known at compile time. 这种类型的数组通常称为动态数组,因为在编译时不需要知道大小。

Dynamic arrays are allocated with new or malloc (though you should only use new in C++). 动态数组是使用new或malloc分配的(尽管您仅应在C ++中使用new)。 These types of array are strictly pointers. 这些类型的阵列的严格的指针。 When I say int* a=new int[4] , I allocate 4 integers: thats it. 当我说int* a=new int[4] ,我分配了4个整数:就是这样。

We achieve mutlidementionality here by crating jagged arrays, which are arrays of pointers. 我们在这里通过创建锯齿状数组(即指针数组)来实现变异。 So for example: 因此,例如:

int** a = new int*[2];
for (int i = 0; i < 2; i++) { a[i]=new int[3];}

What your code does 您的代码做什么

int arr[2][3];//
int (*p1)[2][3] = &arr;  // arr decays to int(*)[3], so we need to take the address
int (*p2)[2][3] = new int[42][2][3]; // allocate a dynamic array (42 elements long) of int[2][3] (sizeof(int[2][3])==24, so this allocates 42*24 bytes!)

It essentially allocates 43 int[2][3] in a row. 它本质上连续分配了43个int [2] [3]。 So it actually comes up with all contiguous memory (though why it needs to be dynamic and not static is beyond me). 因此,它实际上包含所有连续的内存(尽管为什么我需要动态内存而不是静态内存)。

Personally, my rule is multidementional arrays "confusing as hell" and only use them in a local context now, but to each their own. 就我个人而言,我的规则是“使人迷惑”的多维度数组,现在仅在本地上下文中使用它们,但每个数组都使用它们。

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

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