函数局部变量和函数的参数在栈中的布局

版权声明:本文为博主原创文章,未经博主允许不得转载。(注:如有侵权,请发邮件表明文章和原出处地址,我一定在文章中注明) https://blog.csdn.net/zgaoq/article/details/89298258
#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)、函数内的局部变量入栈顺序:按照定义时的顺序。

猜你喜欢

转载自blog.csdn.net/zgaoq/article/details/89298258
今日推荐