I was trying to do something using sizeof operator in c++.
Please refer to the following code snippet.
http://ideone.com//HgGYB
#include <iostream>
using namespace std;
int main()
{
int *pint = new int[5];
int temp = sizeof(*pint);
cout << "Size of the int array is " << temp << endl;
return 0;
}
I was expecting the output as 5*4 = 20. Surprisingly it comes to 4. Any ideas ?
Here is
pintis anint*. So,Compiler doesn’t know about
new int[5], when it doessizeof(*pint)(becausesizeof()is a compile time operator).[Note: Try the same test with statically declared array,
int pint[5];and will see the expected result.Additionally,
sizeof()returnssize_t(which is an unsigned value), so it should be:]