|
发表于 2006-1-4 11:55:57
|
显示全部楼层
不能那样认为,你所说的前一个正常,只是说明刚好那个地址没有被覆盖而已,就像下面这样:- [rick@Fedora-Core test]$ cat test.c
- #include <stdio.h>
- #include <stdlib.h>
- int* test()
- {
- int i = 10;
- int* n = &i;
- return n;
- }
- int main()
- {
- int* x = test();
- printf("lalalalala,%d\n");
- printf("%d\n",*x);
- return 0;
- }
- [rick@Fedora-Core test]$ gcc test.c
- [rick@Fedora-Core test]$ ./a.out
- lalalalala,10
- 10
复制代码
但是,很有可能会被覆盖,就像下面这样:- [rick@Fedora-Core test]$ cat test.c
- #include <stdio.h>
- #include <stdlib.h>
- int* test()
- {
- int i = 10;
- int* n = &i;
- return n;
- }
- int main()
- {
- int* x = test();
- printf("lalalalala,%d\n",1);
- printf("%d\n",*x);
- return 0;
- }
- [rick@Fedora-Core test]$ gcc test.c
- [rick@Fedora-Core test]$ ./a.out
- lalalalala,1
- 1
复制代码 |
|