一些入栈的顺序


(2007-10-08 21:17:32)
 

#include <stdio.h>
#include <iostream>
using namespace std;
struct A
{
 int a;
 int b;
 int c;
};
void fun(int i, float f, double d)
{
 int a = 1;
 int b = 2;
 cout << &d << endl;
 cout << &f << endl;
 cout << &i << endl;
 cout << &a << endl;
 cout << &b << endl;
}
int main(void)
{

 int i = 100;
 char c = 'a';
 float f = 5.0;
 double d = 5.0;
 bool b = true;
 int *pI = &i;
 int **ppI = &pI;
 cout << &i << endl;
 printf("%p/n", &c);
 cout << &f << endl;
 cout << &d << endl;
 cout << &b << endl;
 cout << &pI << endl;
 cout << &ppI << endl;
 cout << endl;
/*
地址: 0013FF7C
  0013FF78
  0013FF74
  0013FF6C
  0013FF68
  0013FF64
  0013FF60 
*/
 fun(i, f, d);
/*
地址: 0013FEE8
  0013FEE4
  0013FEE0
  0013FED4
  0013FED0
*/
 cout << endl;
 int a[5] = {1, 2, 3, 4, 5};
 int m = 7;
 cout << a << endl;
 cout << &a[0] << endl;
 cout << &a[1] << endl;
 cout << &a[2] << endl;
 cout << &a[3] << endl;
 cout << &m << endl;
 cout << endl;
/*
地址: 0013FF4C
  0013FF4C
  0013FF50
  0013FF54
  0013FF58
  0013FF48
*/
 cout << endl;
 char s[] = "123456789"; //10
 char p[] = "123";  // 8
 cout << sizeof(s) << endl;
 cout << sizeof(d) << endl;
 printf("%p/n", &s[0]);
 printf("%p/n", &s[1]);
 printf("%p/n", &s[2]);
 printf("%p/n", &s[3]);
 printf("%p/n", &s[4]);
 printf("%p/n", &s[5]);
 printf("%p/n", &s[6]);
 printf("%p/n", &s[7]);
 printf("%p/n", &s[8]);
 printf("%p/n", &s[9]);
 cout << endl;
 printf("%p/n", &p[0]);
 printf("%p/n", &p[1]);
 printf("%p/n", &p[2]);
 printf("%p/n", &p[3]);
 cout << endl;
/*
地址: &s[0] ~ &s[9]
  0013FF3C....0013FF45
  &p[0] ~ &p[4]
  0013FF38...0013FF3B
*/
 struct A n;
 cout << &n.a << endl;
 cout << &n.b << endl;
 cout << &n.c << endl;
 cout << endl;
/*
地址: 0013FF3C
  0013FF40
  0013FF44
*/ 
 return 0;
}
/*
 栈是从高地址向低地址方向扩展的
1.普通变量按声明的变量顺序入栈,即高地址向低地址依次存放各个声音的变量
2.函数调用时,形参从右向左依次入栈
3.数组内部地址排列可能与平台有关,但是一般从低地址向高地址依次存放各个成员
4.类和结构体内部的数据成员先声明的会被放在高地址还是低地址处,完全由编译器实现来决定的
,而且一般都会采用"按照声明的先后顺序从低地址向高地址依次存放各个成员".
*/

5.基于RISC(精简指令集计算机)的CPU比如SPARC、PowerPC等,对内存中基本数据类型的变量采用高字节和高字在低地址存放、低字节和低字在高地址存放的Big Endian存储格式,并且把高字节的地址作为变量的首地址。

    AMD、Intel 系列的CPU采用Little Endian存储格式来存放基本类型变量,即低字节和低字在低地址放、高字节和高字在高地址存放,并且把最低字节的地址作为变量的首地址。

转载于:https://my.oschina.net/dake/blog/196847

猜你喜欢

转载自blog.csdn.net/weixin_34387468/article/details/91586174