Why does sizeof and _count work on static array but not dynamic array?

I can see that arrays and pointers are fundamentally the same i.e array decomposes to a pointer thus for int x[5], x on its own gives address of x[0] just like if I had used pointer with dynamic memory allocation int *x = new int[5], x on its own would give address of x[0].

Now, why is it that while sizeof and _count work on static arrays, they do not work on dynamic arrays that are allocated using pointer. This question arises as I am not aware of the internal working of sizeof and _count. I am sure it is possible to have a mechanism within the language whereby the sizeof and count are automatically calculated and stored somewhere when a dynamic memory is allocated but no one has done this yet?

NB: I meant using sizeof to find sizeof array
Last edited on
I am sure it is possible to have a mechanism within the language whereby the sizeof and count are automatically calculated and stored somewhere when a dynamic memory is allocated but no one has done this yet?


Yes someone has already done this, think std::vector, std::array instead of raw arrays.

I can see that arrays and pointers are fundamentally the same

There are subtle differences between arrays and pointers. This is what is causing you the problems. You can't use sizeof() to determine the size of the array if you only have a pointer to the array. If you use sizeof() on a pointer you will get the sizeof the pointer, not the size the array.

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.
I am made more curious by the last statement which says "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. ".. interesting
Topic archived. No new replies allowed.