C/C++ 变量内存地址高低位安排

就拿自由变量来说:

1. 自由变量分配地址是从高地址 -> 低地址的(自由变量放在栈中,其他的变量地址都是从低到高分配)

2.多字节的变量,高位在高地址,低位在低地址。


测试: 

#include <stdio.h>

int main ()
{
  int x = 0x00f00fff;
  int y = 0xfff00f00; 
  unsigned char *px1,*px2,*px3,*px4;
  unsigned char *py1,*py2,*py3,*py4;
  
  px1 = (char *)&x + 0;
  px2 = (char *)&x + 1;
  px3 = (char *)&x + 2;
  px4 = (char *)&x + 3;
  
  py1 = (char *)&y + 0;
  py2 = (char *)&y + 1;
  py3 = (char *)&y + 2;
  py4 = (char *)&y + 3;
  
  printf("&x = %08x \n",&x);
  printf("&x = %08x %08x %08x %08x\n",px1,px2,px3,px4);
  printf("x = %08x \n",x);
  printf("x = %x %x %x %x\n",*px1,*px2,*px3,*px4);
  
  printf("&y = %08x \n",&y);
  printf("&y = %08x %08x %08x %08x\n",py1,py2,py3,py4);
  printf("y = %08x \n",y);
  printf("y = %02x %02x %02x %02x\n",*py1,*py2,*py3,*py4);
  
  return 0;
}
C/C++ 变量内存地址高低位安排_第1张图片

你可能感兴趣的:(C/C++ 变量内存地址高低位安排)