函数局部变量和函数的参数在栈中的布局
#include <stdio.h>
#include <iostream>
using namespace std;
void func(int p1, int p2, int p3)
{
int a = p1;
int b = p2;
int c = p3;
std::cout << "函数参数入栈顺序(栈在内存中向上伸长):从右到左" << std::endl;
std::cout << "&p1:" << &p1 << std::endl;
std::cout << "&p2:" << &p2 << std::endl;
std::cout << "&p3:" << &p3 << std::endl;
std::cout << std::endl;
std::cout << "函数内局部变量入栈顺序(栈在内存中向上伸长):从下到上" << std::endl;
std::cout << "&a:" << &a << std::endl;
std::cout << "&b:" << &b << std::endl;
std::cout << "&c:" << &c << std::endl;
}
int main()
{
func(2, 3, 4);
return 0;
}
结果:
结论:谁先入栈谁的地址大
(1)、函数的参数入栈顺序:从右向左。
(2)、函数内的局部变量入栈顺序:按照定义时的顺序。