为什么在Visual C++中结构中的指针出现错误但是GCC没有?

Pei*_*Pei 1 c++ visual-c++

我们使用下面的结构.

struct  S
{
    int i;
    int *p;
};
Run Code Online (Sandbox Code Playgroud)

主要流程:

int main()
{
    S s;
    int *p = &s.i;
    p[0] = 4;
    p[1] = 3;
    printf("p[0]=%d\n", p[0]);
    printf("p[1]=%d\n", p[1]);
    s.p = p;
    s.p[0] = 1;
    s.p[1] = 2;

    printf("p[0]=%d\n", p[0]);
    printf("p[1]=%d\n", p[1]);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

然后我们的进程运行它在sp [1] = 1时发生了内存错误,当我们用Visual C++编译它时.

但是当我们用GCC编译它时它可以运行.

为什么在VC++中出现错误但GCC没有?

Bat*_*eba 7

行为p[1] = 3;未定义的.

p指向的至少两个元件的阵列.