The basic pseudo code looks like this:
void myFunction()
{
int size = 10;
int * MyArray;
MyArray = new int[size];
cout << size << endl;
cout << sizeof(MyArray) << endl;
}
The first cout returns 10, as expected, while the second cout returns 4.
Anyone have an explanation?
MyArrayis only a pointer, which on your system, has a size of four bytes.When you dynamically create an array, you need to keep track of the size yourself.
If you created an automatic array or static array,
then
sizeof(MyArray)would be 40. As soon as the array decays to a pointer, though, e.g. when you pass it to a function, the size information is lost.