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.
Dynamic arrays in C++ are declared using the new keyword. We use square brackets to specify the number of items to be stored in the dynamic array. Once done with the array, we can free up the memory using the delete operator. Use the delete operator with [] to free the memory of all array elements.
Explanation: The number of items used by the dynamic array contents is called logical size. Physical size is the size of the underlying array, which is the maximum size without reallocation of data.
A dynamic array is a random access, variable-size list data structure that allows elements to be added or removed. It is supplied with standard libraries in many modern programming languages. Dynamic arrays overcome a limit of static arrays, which have a fixed capacity that needs to be specified at allocation.
We can find the size of an array using the sizeof() operator as shown: // Finds size of arr[] and stores in 'size' int size = sizeof(arr)/sizeof(arr[0]);
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.
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++.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With