Oct 1, 2010 at 6:29am UTC
Hello ,in the following code i use a double variable and a double pointer and i use the sizeof() function to examine their length.
#include<iostream>
using namespace std;
int main()
{
double some;
double* thing;
cout<<"size of some is:"<<sizeof(some)<<endl;
cout<<"size of thing is:"<<sizeof(thing)<<endl;
return 0;
}
But i can't understant the output:
size of some is:8
size of thing is:4
Why the size function returns less leng for the pointer although both the variable "some" and the pointer "thing" are declared as doubles?Thank you
Oct 1, 2010 at 7:30am UTC
One is a double one is a pointer.
The fact that you declared as "double* thing;" simply means it's a pointer, that points to a double. It's still just a pointer though.
This is why we use pointers, it's cheap, less memory.
Last edited on Oct 1, 2010 at 7:33am UTC
Oct 1, 2010 at 7:31am UTC
because thing only store the address of "double" type
And I guest your os is 32bit's,right?
that is why the size of pointer would be 4byte = 32bits
Oct 1, 2010 at 8:18am UTC
yeap,my os is 32.That's why however i declared i pointer(int,double.etc) the sizeof returns me the same size ,thus 4. Ok got it,thanks you all.
Oct 1, 2010 at 8:24am UTC
Actually unlike Java, the sizeof values can be different depending you compile your C/C++ programs on which platform. Of cuz from what I "play" around with, 4 bytes seem a common value for pointer datatype using Linux gcc/g++.
Anyone report see-ing 8 bytes? Say on a 64bit OS maybe?
Oct 1, 2010 at 12:48pm UTC
Try storing a pointer to a virtual method declared in a class that multiply inherits.
Size of such a pointer will be at least 6.