Last active
November 5, 2016 07:51
-
-
Save itczl22/56b23fb151fa6c8c7173ee59d245fded to your computer and use it in GitHub Desktop.
判断栈的增长方向
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 判断栈的增长方向 | |
* | |
* 系统在调用函数时, 这个函数的相关信息都会出现栈之中, 比如参数、返回地址和局部变量 | |
* 入栈顺序和函数内的变量申明顺序可能不一样, 这和编译器有一定的关系 | |
* 所以不能通过一个函数内的多个变量在栈中的地址来判断栈的增长方向 | |
* | |
* 当它调用另一个函数时, 在它栈信息保持不变的情况下会把它调用那个函数的信息放到栈中 | |
* 两个函数的相关信息位置是固定的, 肯定是先调用的函数其信息先入栈, 后调用的函数其信息后入栈 | |
* 这样通过函数栈来确定栈的增长顺序 | |
*/ | |
#include <stdio.h> | |
void func_2nd(int* val_1st) { | |
int val_2nd = 20; | |
printf("%p\n%p\n", val_1st, &val_2nd); | |
if(val_1st > &val_2nd) { | |
printf("从高到低增长\n"); | |
} else { | |
printf("从低到高增长\n"); | |
} | |
} | |
void func_1st(void) { | |
int val_1st = 10; | |
func_2nd(&val_1st); | |
} | |
int main(void) { | |
func_1st(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment