Question:
C++ quirk: Why sizeof(pointer to array) is not same as sizeof(array)?
anonymous
2013-01-20 06:09:46 UTC
Consider snippet:

int a[5];
int* t=a;
int* u=new int [5];

cout<<"size of a : "< cout<<"size of a[0] : "< cout<<"size of t : "< cout<<"size of t[0] : "< cout<<"size of u : "< cout<<"size of u[0]"<
I have learnt that array variable's names are treated as implicit Pointers like sizeof(a) gives 20 as i expected but why not sizeof(t) which is also the pointer.
Why is it so? Plz Explain the reason.
[I am using G++ on Ubuntu.]
Four answers:
peteams
2013-01-20 06:44:44 UTC
sizeof(x) gives you the size of the parameter as the compiler sees it when it compiles.



C++ inherits the quirk from C that it doesn't really have arrays, just pointer arithmetic. So in your code a and t mostly behave identically. There are several differences though:

- You can assign to t, but not to a. Had you written int*const t they would be more similar.

- The & operator on t will yield the address of t, the & operator on a will return a.

- sizeof(t) returns the size of t, the size of the pointer, whereas sizeof(a) returns the size of the array.



This is how the language works, it is a little inconsistent, but it is necessary for good behaviour.



You will find some implementations offer safe versions of unsafe functions like strcpy and memcpy. These safe versions require you to supply the size of parameters to prevent buffer overruns. They come with templated versions that have the same signature as the unsafe versions and use sizeof on the arrays when passed to automatically supply the sizes.
Cubbi
2013-01-20 19:20:10 UTC
"I have learnt that array variable's names are treated as implicit Pointers"

That is wrong: the name of an array designates the array, not a pointer. You can use the name of an array to refer to the array when calling operators and functions that expect array arguments (sizeof, typeid, operator&, std::swap(), std::begin(), std::end(), etc).



The pointer connection comes from when you try to use that name where arrays are NOT allowed: the implicit conversion to pointer is tried and if pointers are allowed, one is constructed implicitly. This is the same as when you use a double with a function that expects ints: an int is constructed holding the value of the double with fractional part discarded. This isn't grounds to say that the name of a double is streated as implicit int.



That said, of course in C++ we normally use vectors, which fit in with the language a lot better than C's arrays.
no1home2day
2013-01-20 14:21:34 UTC
You asked, "Why sizeof(pointer to array) is not same as sizeof(array)?"



It is because an array is not the same size as the pointer to that array. They are two different "animals".



One is an actual array whereas the other is a POINTER.



Pointers maintain the same size, no matter what they are pointing to.



Hope that helps.
Teja
2013-01-20 14:28:01 UTC
pointer means it points to an address location..so what ever it points is not important and as it is address it is of 4bytes

and by the statement

int* t=a;

u simply mean that t points to array's first element and both t and a are same


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...