how to catch out of memory exception in c++?

后端 未结 4 1475
耶瑟儿~
耶瑟儿~ 2020-12-04 16:46

can anyone please tell me how to catch out of memory exception?

for ex.

try
{
    while(true)
    {
        int i = new int;
    }
}
catch( ? <---         


        
4条回答
  •  独厮守ぢ
    2020-12-04 17:16

    Catch std::bad_alloc.

    You will also need a strategy for handling the errors, since many of the things you'd like to do will require memory (even if it's only to display an error to the user before shutting down). One strategy is to allocate a block of memory at startup, and delete it in the exception handler before attempting to use more memory, so that there is some available to use.

提交回复
热议问题