C/C++语言指针和函数,作用域的问题

#include <iostream>
using namespace std;

void test(int **p){
int c = 10;
*p = &c;
}

int main(){
int a = 5;
int *p = &a;

test(&p);

cout << *p << endl;
return 0;
}

函数结束c变量不应该释放吗?
为什么指针还有值?

你的理解很正确。c这个变量还没来得及释放就去访问,所以没问题,如果在前面故意拖延一下就会出现异常。

#include <iostream>
#include <Windows.h>
using namespace std;

void test(int **p){
    int c = 10;
    *p = &c;
}

int main(){
    int a = 5;
    int *p = &a;

    test(&p);
    system("pause");

    cout << *p << endl;
    return 0;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-10-03
这个时候输出的的确是0啊

有什么问题吗
相似回答