[英]Dynamic array allocation
I'm wondering why new doesn't seem to retain the array information and just returns an int*: 我想知道为什么new似乎没有保留数组信息并只返回一个int *:
#include <iostream>
using namespace std;
typedef int (*p_to_array)[80];
typedef int arr[80];
int main()
{
arr a;
p_to_array p = &a; //fine
p_to_array p2 = new arr; // incompatible types
return 0;
}
I might be totally wrong about this: (ie this is little more than an educated guess) 我可能完全错了:(即这只是一个有根据的猜测)
new arr;
is equivalent to: 相当于:
new int[80];
which by language rules returns int*
. 根据语言规则返回
int*
。
From cppreference.com: (thanks to @interjay for pointing out I actually cited the irrelevant part) 来自cppreference.com :(感谢@interjay指出我实际上引用了无关的部分)
The new-expression returns a prvalue pointer to the constructed object or, if an array of objects was constructed, a pointer to the initial element of the array.
new-expression返回一个指向构造对象的prvalue指针,或者,如果构造了一个对象数组,则返回指向数组初始元素的指针。
My understanding of that is that when allocating an array, the new[]
form will be picked. 我对此的理解是,在分配数组时,将选择
new[]
表单。 The only thing your example changes is the syntax; 你的例子唯一改变的是语法; it doesn't make the language treat arrays as non-arrays (regular objects).
它不会使语言将数组视为非数组(常规对象)。
You can either use the array syntax or a typedef-name to allocate arrays with new
- both is allowed and equivalent according to [expr.new]/5: 您可以使用数组语法或typedef-name来分配带有
new
数组 - 根据[expr.new] / 5,允许和等效:
When the allocated object is an array (that is, the noptr-new-declarator syntax is used or the new-type-id or type-id denotes an array type ), the new-expression yields a pointer to the initial element (if any) of the array .
当分配的对象是数组时(即,使用noptr-new-declarator语法或new-type-id或type-id表示数组类型 ), new-expression产生指向初始元素的指针(如果任何)数组 。
As you can see, for arrays, solely a pointer to the initial element is given; 如您所见,对于数组,只给出了指向初始元素的指针; Not a pointer to the array object itself.
不是指向数组对象本身的指针。 Thus the type yielded is
int*
, not int (*)[N]
. 因此,产生的类型是
int*
,而不是int (*)[N]
。
The reason why a pointer to the initial element is returned is quite simple: It is the pointer that you want to work with. 返回指向初始元素的指针的原因很简单:它是您要使用的指针。
We want to write 我们想写
auto p = new int[50];
// Or
int* p = new int[50];
So as to be able to use normal subscripting: 以便能够使用普通的下标:
p[49] = 0xDEADBEEF;
That would be nonsensical with p
if it was of type int(*)[50]
pointing to the array object, because the subscript would add 49*50*4 to the address, not just 49*4 - because of subscript semantics and pointer arithmetics. 如果它是类型为
int(*)[50]
指向数组对象,那p
将是荒谬的,因为下标将向地址添加49 * 50 * 4,而不仅仅是49 * 4 - 因为下标语义和指针运算。
We would have to either use two subscripts: 我们必须使用两个下标:
p[0][49] = ...;
Which is horrific and unnecessarily redundant. 这是可怕的,不必要的多余。 Or we could use a cast:
或者我们可以使用演员:
int* p = static_cast<int*>(new int[50]); // Or with auto
But that is ugly too. 但那也很难看。
To avoid such hassles, this rule was introduced. 为了避免这种麻烦,引入了这条规则。
In this declaration 在这个宣言中
p_to_array p2 = new arr; // incompatible types
type of initializer new arr;
初始化程序类型
new arr;
does not correspond to the type of variable p2
不对应于变量
p2
的类型
The type of initializer is int *
that is the initializer is pointer to the initial element of the allocated array. 初始化程序的类型是
int *
,初始化程序是指向已分配数组的初始元素的指针。 The type of variable p2
is int (*p_to_array)[80]
according to the typedef definition. 根据typedef定义,变量
p2
的类型是int (*p_to_array)[80]
。
There is no implicit conversion from type int *
to type int (*p_to_array)[80]
. 没有从类型
int *
到int (*p_to_array)[80]
隐式转换。 So the compiler issues an error. 所以编译器发出错误。
The correct declaration could look for example the following way 正确的声明可以通过以下方式查找
p_to_array p2 = new arr[1];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.