It's said that arrays "decay" into pointers. A C++ array declared as int numbers[5] cannot be re-pointed, i.e. you can't say numbers = 0x5a5aff23.
When you pass an array into a function, either directly or with an explicit pointer to that array, it has decayed functionality, in that you lose the ability to call sizeof() on that item, because it essentially becomes a pointer. This is why it's preferred to pass by reference (among other reasons).
When you pass an array into a function, either directly or with an explicit pointer to that array, it has decayed functionality, in that you lose the ability to call sizeof() on that item, because it essentially becomes a pointer. This is why it's preferred to pass by reference (among other reasons).
Three ways to pass in an array:
void by_value(const T array[])
void by_pointer(const T* const array)
void by_reference(const T (&array)[U])
Only the last, the reference example, will give proper sizeof() info.
Edited to add: Note: it's not really "by value", just idiomatically so. The purpose of the three functions is to show the various "techniques" which don't maintain the full functionality of an array, i.e. sizeof(), but instead lead to "decay".
0 comments:
Post a Comment