C++ 即使从函数返回,堆栈内存仍然存在。?
我在一本书中举了一个例子:C++ 即使从函数返回,堆栈内存仍然存在。?,c++,arrays,memory,C++,Arrays,Memory,我在一本书中举了一个例子: int * func() { int A[3][3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; return (int*)A; } void main(void) { int *a = func(); cout << *(a + 2*3 + 1); system("PAUSE"); } int*func() { INTA[3][3]={1,2,3,4,5,6,7,8,9}; 返回(int*
int * func()
{
int A[3][3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
return (int*)A;
}
void main(void)
{
int *a = func();
cout << *(a + 2*3 + 1);
system("PAUSE");
}
int*func()
{
INTA[3][3]={1,2,3,4,5,6,7,8,9};
返回(int*)A;
}
真空总管(真空)
{
int*a=func();
cout你说得很对:代码显示
另一方面,如果A
是static
或heap-allocated,情况可能会大不相同。这是未定义的行为!“这一次你的代码似乎侥幸逃脱了惩罚,你是Wascawy wascal!下次我会抓到你的,Gadget!”(UB不要求在执行时崩溃或失败。也不要求不放火烧当地的尼姑庵。未定义表示未定义。)