Re: new an array of pointers
* James Kanze:
On 18 Mar, 10:05, thomas <freshtho...@gmail.com> wrote:
Hi,
----------
int **p = new int *[30]; //OK
Evaluates as:
int** p = new (int (*([30]));
An array of 30 int*.
int **p = new (int) *[30]; //ERROR
int** p = (new (int)) * [30];
Don't know what the compiler is supposed to make of the [30],
given that there's no address expression to the left of it.
(And the '*' is multiplication.)
int **p = new (int*)[30]; //ERROR
int** p = (new (int*))[30];
Here, the initialization expression is a syntactically legal
expression (but would have undefined behavior at runtime),
new expressions are a wilderness of special case rules. On reading your
statement I thought huh, that must a placement form syntactically. But as it
turned out[1] I was wrong[2] about why you're wrong here,
<example>
"ComeauTest.c", line 3: error: this operator is not allowed at this point; use
parentheses
int **p = new (int*)[30];
^
"ComeauTest.c", line 3: error: a value of type "int *" cannot be used to initialize
an entity of type "int **"
int **p = new (int*)[30];
</example>
but
it doesn't have the type int**: the new expression returns an
int** (pointing to a single uninitialized int*), but
dereferencing it (remember, a[b] is the same as *(a+b)) results
in an int*.
Right, but first it must pass muster syntactically.
Cheers,
- Alf
Notes:
[1] Also g++ and MSVC object to the construct; g++ like Comeau.
[2] Yet another case where coffee is called for.