How much null checking is enough?

后端 未结 18 1148
清酒与你
清酒与你 2021-01-30 01:10

What are some guidelines for when it is not necessary to check for a null?

A lot of the inherited code I\'ve been working on as of late has null-checks

18条回答
  •  甜味超标
    2021-01-30 01:12

    Older versions of Microsoft C++ (and probably others) did not throw an exception for failed allocations via new, but returned NULL. Code that had to run in both standard-conforming and older versions would have the redundant checking that you point out in your first example.

    It would be cleaner to make all failed allocations follow the same code path:

    if(obj==NULL)
        throw std::bad_alloc();
    

提交回复
热议问题