1
这是我关于如何打印方法函数的局部变量的 主要方法价值教练我的朋友的问题时,它的 变量推,并从弹出堆栈(因为当法功能 称为它的变量推当它达到从 堆栈结束弹出),然后它是本地变量存储回内存。全局指针
为什么主要方法打印100?
// Define a global pointer
int *ptr;
int method()
{
// Define a variable local in this method
int local = 100;
// Set address of local variable (name of variable is local)
// in the ptr pointer
ptr = &local;
return -1;
}
int main()
{
// Call method
method();
// Print value of ptr pointer
cout<<*ptr<<"\n";
return -1;
}
我认为这个想法是特定于操作系统。我在unix中看到了这种行为。 –
不要这样想。我在Windows中看到了这种行为,并且据我所知只是一般的C概念 – cjds