How to check memory allocation failures with new operator?

前端 未结 4 1118
囚心锁ツ
囚心锁ツ 2020-12-03 10:16

Just recently I switched the language of my project to use C++ from C. With C, I used malloc and after that I check if malloc was successful but with C++, I use \'new\' to a

4条回答
  •  暖寄归人
    2020-12-03 10:48

    I hate to say it, but IMO, you're going in the wrong direction (and, unfortunately, the other answers you've gotten haven't really pointed you in the right direction either).

    Rather than choosing between different varieties of smart pointer and/or normal vs. nothrow variants of new, you should probably take at least two more steps back from what you're doing, and replace your manually-managed dynamic data structures with collections. This may not always be the right choice, but at least in my experience, it's the right way to go a lot more often than not. The standard library has a number of possibilities (vector, deque, list, set, etc.), and chances are pretty good that you can use one of them rather than dealing directly with new and company at all.

    By default, those will use an allocator that ends up using the normal (throwing) variant of new. You, therefore, normally want to put most code in a try block at a fairly high level, and have a catch clause that deals with having run out of memory there.

    When/if you do need to deal with allocating memory directly, chances are pretty good that you still want to provide an interface similar to that of the standard containers in the library so it'll work with the normal algorithms and iterators. Your initial experience using the existing containers will pay of well when you get to this point, even though it may be a ways down the road.

提交回复
热议问题