2 ms·
Do I need to check for NULL pointers?
I am really confused right now, I have read this :
https://medium.com/pvs-studio/why-it-is-important-to-check-what-the-malloc-function-returned-ba39f3d13a83
Although I don't like the weird tone, it gives a good enough reason to check for NULL pointers but I also read that :
https://membarrier.wordpress.com/2020/11/08/stop-checking-for-null-pointers/
Hard to make up my mind. Help.
- viraptor 6y agoThose posts are not conflicting. The first one is about checking the result of malloc, the second one about checking a pointer argument to your own function. They're both correct.
- simonblack 6y agoThe best answer is 'Yes'. But in practical terms, I suppose it depends on whether you need that pointer to be NOT-NULL. If you're never ever going to use that pointer, you probably will get away with it. But it's also a bit like 'If you never lie, you don't need a good memory'. If you always test for NULL, it's never going to blow up in your face when you least expect it.