程序1
#include <stdio.h>
#include <windows.h>
#pragma warning (disable:4996)
//1.栈区的内存自动申请释放,不需要程序编写者管理
int* test1(){
int a = 5;
return &a;
}
int test2(){
//不需要关注值是多少,是因为局部变量a的内存已经被回收
int* b = test1();
printf("%d",*b);
}
int main()
{
test2();
}
程序1对应的图
![](https://img-blog.csdnimg.cn/img_convert/a076cfd27f0934a437d28c6059907073.webp?x-oss-process=image/format,png)
image.png
程序2
#include <stdio.h>
#include <windows.h>
#pragma warning (disable:4996)
//1.栈区的内存自动申请释放,不需要程序编写者管理
char* test1(){
char a[] ="helloword";
return a;
}
int test2(){
//不需要关注值是多少,是因为局部变量a的内存已经被回收
char* b = test1();
printf("%s",b);
}
int main()
{
test2();
}
程序2对应的图
![](https://img-blog.csdnimg.cn/img_convert/24ee165b9975cdf0a30d331716d34ee8.webp?x-oss-process=image/format,png)
image.png
程序3
#include <stdio.h>
#include <windows.h>
#pragma warning (disable:4996)
//1.栈区的内存自动申请释放,不需要程序编写者管理
char* test1(){
//在这里和程序2不同 主要是 返回的是指针,指针指向的常量区...所以把常量区地址赋值使用 ,怎么也没事啊
char *a ="helloword";
return a;
}
int test2(){
//不需要关注值是多少,是因为局部变量a的内存已经被回收
char* b = test1();
printf("%s",b);
}
int main()
{
test2();
}
程序3的图
![](https://img-blog.csdnimg.cn/img_convert/1ab722e00a71576dae8827b86922abc0.webp?x-oss-process=image/format,png)
image.png