Fun with NULL pointers, part 2
Fun with NULL pointers, part 2
Posted Jul 22, 2009 10:19 UTC (Wed) by i3839 (guest, #31386)In reply to: Fun with NULL pointers, part 2 by epa
Parent article: Fun with NULL pointers, part 2
If you pass along a pointer and say "hey, do something with this", in general that code did something with it before, otherwise the second function could be called directly.
For pointers within structures it's much harder though, then it helps to have clean code with clear lifetime rules. If it's messy enough you can make things more messy by adding NULL checks everywhere. If this adds more unexpected error paths then it's not a good way forwards though. If it's unclear if something can be NULL or not then in the long term it's much better to clean up the code. Like assertions BUG_ON is good while developing a new piece of code to make sure all assumptions hold. When the code gets more into shape and becomes more stable there should be not much need for such checks anymore.
An exception is low level library like code which can be used by many users and which wants to prevent them making the same bloody mistakes. It's also a good way to prevent corruptions from spreading around.
The further away the code giving the input is from the code using it the more checking is necessary.
