1 #include <stdio.h>
2 #include <stdlib.h>
3
4 void bug()
5 {
6 system("reboot");
7 exit(0);
8 }
9 int stack_test(int a,int b)
10 {
11 //int *p=&a;
12 // p--;
13 // *p=bug;
14 printf("before write : 0x%x\n", b);
15 int *p=&a;
16 p++;
17 *p=0xdddd;
18 printf("after write : 0x%x\n", b);
19 int c=0xcccc;
20 return c;
21 }
22 int main()
23 {
24 int a=0xaaaa;
25 int b=0xbbbb;
26 int ret=stack_test(a,b);
27 printf("you should run here\n");
28 return 0;
29 }
程序运行结果为:
before write:0xbbbb
after write: 0xdddd
you should run here
bug函数重启了系统;
b的值改变的原因:函数参数在进行压栈时自右向左,所以b在下a在上,指针p指向a ,p在进行++时便指向了b,所以改变指针p的值便改变了b的值
转载于:https://blog.51cto.com/ab3813/1761446