Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 即使从函数返回,堆栈内存仍然存在。?_C++_Arrays_Memory - Fatal编程技术网

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不要求在执行时崩溃或失败。也不要求不放火烧当地的尼姑庵。未定义表示未定义。)