[...]
and his type is "int [100]", so of course a type of "int [0]"
would not have any meaning. In the second case, on the other
hand, we are just allocating some memory through the operator
new for the double.
And the type of the memory we allocate is int[0].
Well, that is not so clear according to me. I mean, probably the most
rigorous way to see the problem is that one, but you can't access to
that type if it is in the dynamic memory, so basically the real type is
hidden.
The fact that you cannot access it is linked to this type. If
the type were int[1], then p[0] would be legal. Since the type
is int[0], it's not.
Of course, the result of the new expression is type int*, so
some vital type information has been lost.
You will have just a pointer.
The new expression returns a pointer. The memory which it
allocates has a type, however, which is not the type of the
pointer. (This is how new differs from malloc, for example.)
Additionally, we don't have any
dynamic type for anything but virtual classes, so there is no int [n]
type if n is not constant. Of course IMHO.
That's an interesting observation. The problem is that "dynamic
type" can mean different things. In this case, if I do
something like:
int* p = new int[ i ] ;
each invocation will construct an array with a specific size; an
array with a specific size has a type which includes that size.
Similarly, every object has a type, even if you cannot always
access the information. The case where i is 0 is interesting,
because arguably, you don't have an object.
I still find it cleaner to think of it as having a specific
type. In the general case, it is the type of the actual array
which determines the legal bounds, and I don't see why int[0]
should be an exception: the legal bounds for a T[N] are [0,N)
(and if N is 0, of course, [0,N) is empty).