What is the "new int [];" do?

What does this line of code do?

new int[];

According to my compiler disassembly (VC ++ 2012), it does the same thing:

new int[0];

But is it defined by the C ++ standard? And is this a legal instruction?

+4
source share
2 answers

The expression is new int[]not valid with C ++ 11 (I have not tested C ++ 14).

Using the only []syntax required to copy the (implicitly convertible to) integral expression type between the brackets, indicating the desired size of the array.

Note that this size should not be constant: at the lower level of abstraction, this is how you allocate a dynamic array in C ++.


++ 11 ( N3290) §5.3.4/6:

" noptr-new-declarator (5.19) . noptr-new-declarator , , (12.3)

[]. [] (, ), .


, new std::vector (, , std::string).

+8
new int[]; 

.

n3337 § 5.3.4, [] , - ( ), :

noptr-new-declarator:
     [ expression ] attribute-specifier-seqoptnoptr-new-declarator [ constant-expression ] attribute-specifier-seqopt

, 6:

noptr-new-declarator (5.19) std:: size_t . noptr-new-declarator std:: size_t.

+7