使用new的初始化数组 [英] Initialized arrays using new
问题描述
这会产生一个初始化数组为零:
int * i = new int [100]();
>
004124B0 push ebp
004124B1 mov ebp,esp
004124B3 mov eax,dword ptr [count]
004124B6 push eax
004124B7致电运营商新(411212h)
004124BC加esp,4
004124BF pop ebp
但这并没有初始化阵列。装配输出是相同的。
发生了什么?
int * i = new int [100];
>
004124B0 push ebp
004124B1 mov ebp,esp
004124B3 mov eax,dword ptr [count]
004124B6 push eax
004124B7致电运营商新(411212h)
004124BC加esp,4
004124BF pop ebp
This produces an initialized array to zero:
int *i = new int[100]() ;
004124B0 push ebp
004124B1 mov ebp,esp
004124B3 mov eax,dword ptr [count]
004124B6 push eax
004124B7 call operator new (411212h)
004124BC add esp,4
004124BF pop ebp
But this doesn''t initialize the array. The assembly output is identical.
What''s going on?
int *i = new int[100] ;
004124B0 push ebp
004124B1 mov ebp,esp
004124B3 mov eax,dword ptr [count]
004124B6 push eax
004124B7 call operator new (411212h)
004124BC add esp,4
004124BF pop ebp
推荐答案
John写道:
John wrote:
这会产生一个初始化数组为零:
int * i = new int [100]();
This produces an initialized array to zero:
int *i = new int[100]() ;
这会做什么?
int * i = new int [100](42);
-
Phlip
http://c2.com/cgi/wiki?ZeekLand < - 不是博客!!!
What would this do?
int *i = new int[100](42) ;
--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Phlip写道:
Phlip wrote:
John写道:
John wrote:
>这会使初始化数组为零:
> int * i = new int [100]();
>This produces an initialized array to zero:
int *i = new int[100]() ;
这会做什么?
int * i = new int [100](42);
What would this do?
int *i = new int[100](42) ;
产生编译器错误:
错误C2075:''运算符new()''的目标:数组初始化需要花括号
Produce a compiler error:
error C2075: ''Target of operator new()'' : array initialization needs curly braces
John写道:
John wrote:
> >这会将初始化数组生成为零:
int * i = new int [100]();
>>This produces an initialized array to zero:
int *i = new int[100]() ;
这会做什么?
int * i = new int [100](42);
What would this do?
int *i = new int[100](42) ;
产生编译错误:
错误C2075:''运营商新目标()'':数组初始化需要卷曲
括号
Produce a compiler error:
error C2075: ''Target of operator new()'' : array initialization needs curly
braces
我知道这个断言通过了:
断言(0 == int());
看起来如果''new int [100](42)''无法创建100个整数的数组
初始化为42,然后''new int [100]()''与使用new相同不同
创建一个100''int()数组''结构。似乎尾随
''()''是''new'作为一种函数的状态的工件。您可以说''新
SimCity;''或''新模拟城市()''。
语言律师可能会发现有关该主题的实际措辞,但我会
只需要使用''std :: vector''和''std :: fill()''。
-
Phlip
http://c2.com / cgi / wiki?ZeekLand < - 不是博客!!!
I am aware that this assertion passes:
assert(0 == int());
but it looks like if ''new int[100](42)'' cannot create an array of 100 ints
initialized to 42, then ''new int[100]()'' is not the same as using new to
create an array of 100 ''int()'' constructions. It seems that the trailing
''()'' is an artifact of ''new''s status as a kind of function. You can say ''new
SimCity;'' or ''new SimCity()''.
A language lawyer might find actual verbiage on the subject, but I would
just go with a ''std::vector'' and ''std::fill()''.
--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
这篇关于使用new的初始化数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!