1. 程式人生 > >c語言區域性變數的記憶體地址分配順序

c語言區域性變數的記憶體地址分配順序

測試程式:

#include <stdio.h>

int main(){
    unsigned int ui = 10;
    char c = 11;
    unsigned char uc = 12;
    int i = 13;
    double d = 14;
    short s = 15;

    printf("ok\n");
    printf("%p\n%p\n%p\n%p\n%p\n%p\n", &ui, &c, &uc, &i, &s, &d);
    return 0;
}

Windows下:

  • 先定義先入棧;

輸出:

ok
0062FEAC
0062FEAB
0062FEAA
0062FEA4
0062FE96
0062FE98

Linux下:

  • 按位元組大小,大的先入棧;
  • 位元組大小相同的,後定義的先入棧;
    輸出:
ok
0x7fffee63bad8
0x7fffee63bad4
0x7fffee63bad5
0x7fffee63badc
0x7fffee63bad6
0x7fffee63bae0

分佈圖:

棧頂
c
uc
s
ui
i
d
棧底