Can the C++ `new` operator ever throw an exception in real life?

前端 未结 18 1785
隐瞒了意图╮
隐瞒了意图╮ 2021-01-30 08:40

Can the new operator throw an exception in real life?

And if so, do I have any options for handling such an exception apart from killing my application?

18条回答
  •  有刺的猬
    2021-01-30 09:10

    Note that in Windows, very large new/mallocs will just allocate from virtual memory. In practice, your machine will crash before you see that exception.

    char *pCrashMyMachine = new char[TWO_GIGABYTES];
    

    Try it if you dare!

提交回复
热议问题