栈区

2019-08-12  本文已影响0人  记事本的记事本

程序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对应的图


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对应的图


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的图


image.png
上一篇 下一篇

猜你喜欢

热点阅读