Class members address

Category: C/C++ -> C++ Author: arm11org Date: 2008-02-22 15:26:32
 
arm11org
2008-02-22 15:26:32
class example
{private:
int a;
int b;

};
class data members are stored in the stack while the stack growth is down, so why b is the address (int *) & a +1, instead of (int *) & a-1 do?
siwei_yinxiang
2008-02-22 15:41:21


Here are two structures you address code? A member of the address? Not
q812884245
2008-02-22 15:48:58

LZ give you drew a map

help understand

tong1234
2008-02-22 15:58:39
compiler will arrange for the correct order
key_2046
2008-02-22 16:09:08
you do not reflect the stack address order
mtxiaoyu
2008-02-22 16:26:18
growth is down does not mean that the memory address value decreases the subjective

look book stack memory map it
hhhwww523
2008-02-22 16:28:53
data stored in a heap or stack, depending on the way your application memory for malloc / new out of memory in the heap, For A a; such are defined by the stack! ! !

class data members are stored in the stack --------- lz this sentence obviously wrong, all subsequent conclusions certainly not ~ ~ ~ ~
shuagmajiashi
2008-02-22 16:40:08
can not say class data members are stored in the stack.

in the stack can only store temporary objects, object data members of the storage order you do not know

so stack storage order represents an object order does not reflect the order of the object data members!
mayayun
2008-02-22 16:52:25
I see elsewhere
"stack is made from high address to low address growth (backwards long), but for the structure, the front member's address is smaller than the back member address (being a put), but when successive define two structures, the back should be less than the first address in front of the first address. "
fact is not that so?
YISHENGYOUNI9527
2008-02-22 17:00:12
I made a test:
class example
{
public:
int a1, b1;
};
example1 test1, test2;
cout << "test1 :: a1 Address:" < ; <& test1.a1 << endl;
cout << "test1 :: b1 address:" << & test1.b1 << endl;
cout << "test2 :: a1 address:" << & test2.a1 ; << endl;
cout << "test2 :: b2 address:" << & test2.b1 << endl;
result:
test1 :: a1 address: 0012FF5C
test1 :: b1 address: 0012FF60
test2 :: a2 address: 0012FF4C
test2 :: b2 address: 0012FF50
class and the structure is the same as
jjzzxx51
2008-02-22 17:04:06

reasonable, and my test results are consistent