4

What does this line of code do?

new int[];

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

new int[0];

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

NPS
  • 6,003
  • 11
  • 53
  • 90
  • 5
    Lightning doesn't strike twice and all http://stackoverflow.com/questions/1087042/c-new-int0-will-it-allocate-memory – Yann Oct 16 '14 at 12:28
  • 1
    Sounds like either a bug or an extension. –  Oct 16 '14 at 12:31
  • AFAIK that's a Visual C++ bug. There is a related bug where it (in some contexts) accepts the syntax `delete[n] p`. I've reported the latter. – Cheers and hth. - Alf Oct 16 '14 at 12:31
  • 5
    Seriously, how can a question about `new int[]` be a duplicate of one about `new int[0]`, when the former is never even mentioned in the latter? – Angew is no longer proud of SO Oct 16 '14 at 12:31
  • 1
    **Oh no**, in addition to sillyvoting, now an incorrect answer selected as the "solution". @NPS: please do reconsider your choice. People landing here by googling will be misinformed with the current choice. – Cheers and hth. - Alf Oct 16 '14 at 12:42
  • 2
    @Cheersandhth.-Alf From what I understood "new int[];" isn't legal. If you're conviced it is please provide your own answer and quote the standard - I'll be happy to reconsider my choice. – NPS Oct 16 '14 at 12:45
  • 1
    @NPS Excuse Alf, he's often unclear in his saint anger :) What he means is that the answer is incorrect by saying *"[] requires constant expression"*, which whould mean you couldn't do `new int[i]`, where `i` is a runtime value. That would beat the purpose of dynamic initialization. – jrok Oct 16 '14 at 12:47
  • I was wondering : what is the point of such code? – BЈовић Oct 16 '14 at 13:01
  • @jrok: I fail to see how it can be unclear to both state that it's a bug (in the second comment to the question) and citing the relevent text of the answer in my downvote comment. There is no way to make it more clear that I know of, except by repetition, which you helped with. Maybe that's needed though, repeating everything a number of times. – Cheers and hth. - Alf Oct 16 '14 at 13:02

2 Answers2

8

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

With a single [] the syntax requires an (implicitly convertible to) integral type expression between the brackets, denoting the desired array size.

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


C++11 (via the N3290 final draft) §5.3.4/6:

Every constant-expression in a noptr-new-declarator shall be an integral constant expression (5.19) and evaluate to a strictly positive value. The expression in a noptr-new-declarator shall be of integral type, unscoped enumeration type, or a class type for which a single non-explicit conversion function to integral or unscoped enumeration type exists (12.3)

expression is used for the first [] brackets. In subsequent [] brackets one must use a constant-expression (value known at compile time) because addressing of the outermost array dimension requires known size array items.


Of course, instead of using new directly you will generally be better off using a std::vector (or maybe std::string).

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
7
new int[]; 

is not legal.

As per the draft standard n3337 § 5.3.4, [] should have an expression for the first dimension, and a constant-expression for each subsequent dimension (if any), as dictated by the grammar:

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

Here, every constant-expression shall be a converted constant expression, as specified in clause 6:

Every constant-expression in a noptr-new-declarator shall be a converted constant expression (5.19) of type std::size_t and shall evaluate to a strictly positive value. The expression in a noptr-new-declarator is implicitly converted to std::size_t.

P0W
  • 46,614
  • 9
  • 72
  • 119
  • And standard quote is where? – ThomasMcLeod Oct 16 '14 at 12:32
  • It doesn't have to be a constant expression. Perhaps more relevant is the grammar specification for *noptr-new-declarator*, requiring an expression inside the `[]`. – Mike Seymour Oct 16 '14 at 12:34
  • 1
    **-1** "should have a constant-expression" is incorrect. Which anyone can understand just by *reflecting* a bit. Like, for 1 second or so. – Cheers and hth. - Alf Oct 16 '14 at 12:34
  • The grammar says *constant-expression*, which doesn't mean "constant expression". Perhaps it'd be better if you italicized it to make it clear. –  Oct 16 '14 at 12:37
  • 2
    @remyabel *const-expression* means what it means. But it's only required when you're dynamically allocating arrays, e.g. `new int[i][5];` Means you can't have a non-constant expressions in place of `5`. – jrok Oct 16 '14 at 12:44
  • 1
    @Cheersandhth.-Alf I'm no C/C++ expert, nor have any intention to become in near future, please add your own answer, or edit this. Otherwise I'll be more then happy to delete and learn more, thanks ! – P0W Oct 16 '14 at 12:46
  • @P0W "[] should have constant-expression" is incorrect if you think about it for a second. – jrok Oct 16 '14 at 12:48
  • @jrok yeah I get that, but I'm "out of words" to re-word it :( – P0W Oct 16 '14 at 12:50
  • @Cheersandhth.-Alf It would be only fair to remove your downvote now :) – jrok Oct 16 '14 at 12:54
  • @jrok: I usually remove downvote when an answer is corrected. I even note that in a comment. But here the standard quote is non-attributed, and doesn't match my C++11. I guess it's C++14 or something. I'll wait until it's corrected or attributed. – Cheers and hth. - Alf Oct 16 '14 at 12:56
  • @Cheersandhth.-Alf Thank you ! you've my +1, jrok amd remyabel thanks a bunch for edits. – P0W Oct 16 '14 at 13:00
  • @cheers Which draft are you using? It appears to me that 5.3.4/6 for [n3242](http://www.open-std.org/Jtc1/sc22/wg21/docs/papers/2011/n3242.pdf) and [n3337](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf) are exactly the same. –  Oct 16 '14 at 13:05
  • @P0W: here's the standardeese from N3337: "Every constant-expression in a noptr-new-declarator shall be an integral constant expression (5.19) and evaluate to a strictly positive value. The expression in a noptr-new-declarator shall be of integral type, unscoped enumeration type, or a class type for which a single non-explicit conversion function to integral or unscoped enumeration type exists (12.3" AFAICS it is identical to C++11 (N3290). It does not match the quoted text in the answer. – Cheers and hth. - Alf Oct 16 '14 at 13:08