Friday, 23 August 2013

Why the memory allocation like this when the const reference release to a different type/

Why the memory allocation like this when the const reference release to a
different type/

my compiler is g++ 4.7.3
long i = 2222;
const long& lref = i;
const int& iref = i;
printf("i=%p lref=%p iref=%p \n", &i , &lref, &iref);
the result is
i=0xbfd78760 lref=0xbfd78760 iref=0xbfd78764
why the address of iref is higher than i I think it may just like this:
when const int& reference to long, it like
int temp = i
const int& iref = temp;
but however when th code like
long i = 2222;
const long& lref = i;
const int& iref = i;
int a = 10;
int b = 10;
printf("i=%p lref=%p iref=%p a=%p b=%p\n", &i , &lref, &iref, &a, &b);
result is
i=0xbfade768 lref=0xbfade768 iref=0xbfade774 a=0xbfade76c b=0xbfade770
why the address of a and b is lower than iref in the stack??
and when the code like
long i = 2222;
const long& lref = i;
const int& iref = i;
printf("i=%p lref=%p iref=%p \n", &i , &lref, &iref);
the result is
i=0xbfbe3f84 lref=0xbfbe3f84 iref=0xbfbe3f83
why when the type of iref is char the address of iref is lower than i??
can somebody tell my why? thanks you!

No comments:

Post a Comment