dynamic arrays that are allocated using pointer. |
Semantically, the allocation does not
use a pointer. This is syntactically valid:
int main( int argc, char ** ) {
new double[argc];
return 0;
}[/code]
No pointers were used in the above program.
(Side-effect: a memory leak.)
The main point of the issue is in the words
static and
dynamic.
When the compiler reads a static array declaration, it does memorize the static size and thus whenever that array is referenced, the compiler has the size at its disposal.
1 2
|
int foo[42];
int s = sizeof(foo);
|
The compiler can and does evaluate the
sizeof(foo)
during compilation, because it does have all the components of that constexpr at its disposal.
The compiler cannot possibly know the size of the unnamed dynamically allocated memory block in my example program.
A pointer datatype is clearly defined and does not contain space for size info (apart from the type of the pointed to type, which is a static value). For example, a pointer does not know whether the address stored in pointer is of a single value or of an element of some array.
Yes, the system that actually allocates memory, does have the size (in order to perform array deallocation and for heap bookkeeping), but there is apparently no way to transfer that knowledge.