455,513 Members | 1,611 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,513 IT Pros & Developers. It's quick & easy.

# stack increase direction and big-endian or little-endia

 P: n/a Hi, 1. About all C/C++ compilers, Does stack increase from high address to low address and heap grow increase from low to high? What on earth decides their increase direction, CPU architecture, OS or compiler? 2. In GNU gcc, { int a[] = {0, 1, 2, 3, 4}; bool b; float c; for (int i = 0; i < 5; i++) { cout << hex << a+i << "," ; } cout << endl; cout << hex << &a << "," << &b << "," << &c << endl; } one print result is: 0x22ff40, 0x22ff44, 0x22ff48, 0x22ff4c, 0x22ff50 0x22ff40, 0x22ff3f, 0x22ff38 So considering question one, why the address of the array(a) increased to high address? 3. What decides the big-endian and little-endian, CPU architecture, OS or compiler? 4. { int x = 0x12345678; char *cp = (char*)&x; for(int i = 0; i < 4; i++) { cout << hex << (int)*(cp+i) << " "; } } what is run result? I think it depends on two aspects: stack increase direction and big-endian or little-endia. Considering in the case of big-endian, suppose the address of x is 0x22ff44, namely &x=0x22ff44, and sizeof(x)=4. If stack increases to low address, the variable x occupied 0x22ff44, 0x22ff43, 0x22ff42 and 0x22ff41, so the result is "78 56 34 12". If stack increases to high address, the variable x occupied 0x22ff44, 0x22ff45, 0x22ff46 and 0x22ff47, so the result is "12 34 56 78". Please give me some advice, thank you. Oct 23 '05 #1