The correct way to signal that a pointer doesn't point to valid memory is with a nullptr
. So your return
statement in the case that the memory allocation fails would simply be:
return nullptr;
Of course, the caller of the function needs to make sure that the returned pointer is not nullptr
before they try to dereference it.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…