Why don't I get a segmentation fault when I write beyond the end of an array?

后端 未结 4 1792
闹比i
闹比i 2020-11-22 10:11

why is this not giving error when I compile?

#include 
using namespace std;

int main()
{
    int *a = new int[2];
    // int a[2]; // even t         


        
4条回答
  •  梦谈多话
    2020-11-22 10:45

    Because undefined behavior == anything can happen. You're unlucky that it doesn't crash, this sort of behavior can potentially hide bugs.

    As for a being define twice - that's a bug in the compiler.

提交回复
热议问题