作者shps90060505 (藍嵐)
看板EE_DSnP
標題[請益] stack memory alignment的問題
時間Wed Oct 10 00:25:06 2012
對於memory alignment的問題,
老師曾在課堂上給過例子,
在Stack中的記憶體位置是往下排,
但我寫的code卻往上排@@ 不知發生了什麼事?
還請大家幫忙解答
code和執行結果如下:
---------------------------code-------------------------------
#include<iostream>
using namespace std;
void print(int a, int b){
int c;
int d;
cout<< "address of a and b: "<< &a << " " << &b << endl;
cout<< "address of c and d: "<< &c << " " << &d << endl;
}
int main()
{
int e;
int f;
print(10,20);
cout<< "address of e and f: "<< &e << " " << &f << endl;
return 0;
}
--------------------------------------------------------------
----------------------------result----------------------------
address of a and b: 0x7fff2497afd
c 0x7fff2497afd
8
address of c and d: 0x7fff2497afe
8 0x7fff2497afe
c
address of e and f: 0x7fff2497b00
8 0x7fff2497b00
c
---------------------------------------------------------------
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.218.188
1F:→ shps90060505:自己回自己...因為他們都是int所以往上排 10/10 00:30
2F:→ shps90060505:如果是int c; double d;那就會變往下排 10/10 00:30
3F:→ shps90060505:不過function的參數我目前還沒辦法解釋 只提供現象.. 10/10 00:31
4F:推 timrau:function parameters在標準裡並沒有規定它們怎麼排 10/10 01:30