小编Pei*_*Pei的帖子

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

我们使用下面的结构.

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没有?

c++ visual-c++

1
推荐指数
1
解决办法
72
查看次数

标签 统计

c++ ×1

visual-c++ ×1