10

The following simple program snippet gives compilation errors with gcc-4.3.4.

Program:

int main() 
{   
    char *ptr = new char[10];     
    char *ptr1 = new char[];      
    return 0; 
}  

Compilation errors:

prog.cpp: In function ‘int main()’:
prog.cpp:4: error: expected primary-expression before ‘]’ token
prog.cpp:3: warning: unused variable ‘ptr’
prog.cpp:4: warning: unused variable ‘ptr1’

But the same compiles cleanly with MSVC without any diagnostic message.

So my question is:
Does the Standard allow an new [] to be called without specifying the size? Or this a bug in MSVC?
Can someone provide a reference from the standard which will conclusively say that the above code example is ill-formed or well-formed?


I have had a look at:

5.3.4 New [expr.new] &
18.4.1.2 Array forms [lib.new.delete.array]

but couldnt find any conclusive evidence about the behavior.


EDIT:
Adding the Language Lawyer tag.
I am expecting the answer for an observed behavior regardless of whether it is useful or not, I am fully aware it is not useful nor recommended.

3
  • 1
    If it's legal I don't see the point of it... Commented Mar 20, 2012 at 5:27
  • 2
    I don't see how it could be well formed code? What is the compiler going to do, guess the size? That seems rather difficult (if not impossible). Commented Mar 20, 2012 at 5:27
  • does new char[] allocate memory? i think it creates only a pointer Commented Mar 20, 2012 at 5:31

2 Answers 2

4

This is not syntactically correct.

Have a look at the syntax for a new-expression.

A noptr-new-declarator must contain an expression between the square brackets, and an expression must have a token in it.

Sign up to request clarification or add additional context in comments.

Comments

4

That is not legal c++.

5.3.4 New [expr.new] shows what are legal ways to call new in a big list, which contains this line :

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

and later it explains what the constant-expression can be (in 5.4.3/6 and 5.4.3/7) :

Every constant-expression in a noptr-new-declarator shall be an integral constant expression (5.19) and evaluate to a strictly positive value.


After some thoughts, next items should be relavant :

8.3.4/1 [dcl.array], these parts :

In a declaration T D where D has the form

    D1 [ constant-expressionopt ] attribute-specifier-seqopt

and the type of the identifier in the declaration T D1 is “derived-declarator-type-list T”, then the type of the identifier of D is an array type;

and

if the constant expression is omitted, the type of the identifier of D is “derived-declarator-type- list array of unknown bound of T”, an incomplete object type.

5.3.4/1 tells :

This type shall be a complete object type, but not an abstract class type or array thereof

Since you omitted the array size, the type is not complete, and your program is not valid c++.

11 Comments

What about <expression> in the first line. The <constant-expression> is for multi-dimensional arrays. eg x = new char[plop*5 /*expression */][10 /*const-expression*/][100 /* const-expression */]
With a quick glance I can not see <expression> being empty but ?
@LokiAstari That is why I added what the constant-expression can be in edit : Every constant-expression in a noptr-new-declarator shall be an integral constant expression. Since it is omitted in the question, then it is not a standard c++, but a compiler extension. It's not an UB, since it compiles, but it shouldn't.
@VJovic: Since the program violates a syntax rule, a diagnostic is required; a compiler that doesn't issue a diagnostic is non-conforming. But a conforming compiler could still accept it after issuing a diagnostic (a warning is sufficient). Once it's accepted it, the program's behavior is undefined (because nothing in the Standard defines its behavior).
@LokiAstari: Only if it's a documented extension. By issuing the diagnostic, the compiler has fulfilled its commitment to the standard. ("Implementation defined" means that the implementation must document the behavior.)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.