이가능한 중복 :
Returning the address of local or temporary variable
Can a local variable's memory be accessed outside its scope?무엇이 잘못 되었나요? 매달린 포인터?
#include<iostream>
using namespace std;
int *p = NULL;
void
fun(void){
int i = 10;
p = &i;
}
int
main(void){
fun();
cout<<*p<<endl; //#1
cout<<*p<<endl; //#2
return 0;
}
내가 # 1과 # 2가 출력이 동일한 생각하지만, 왜 # 1 출력 10 및 # 2 출력 임의 번호?
파괴 된 스택 변수를 포인터로 가리키고 포인터를 역 참조하여 정의되지 않은 동작이 발생합니다. –
예상되는 결과 (http://ideone.com/VMYzG) 또는 일부 다른 결과 (http://codepad.org/OiN8n1L0) – UncleBens