Stop checking for NULL pointers! 

when passing a pointer to a C function, there is only one semantically-correct value for that pointer, as opposed to 264-1 invalid values. NULL may or may not be one of these invalid values. Let the MMU and operating system handle it.

Stop checking for NULL pointers! 

@danso ... I'll definitely take a well-defined null-pointer crash over trying to debug a tree of functions returning "something somewhere was invalid"; nice article btw.

What's a bit more debatable is the slightly different case of... if you're a function that doesn't itself dereference it. Would you want to crash on a null pointer as early as possible (... closer to its source) or just go ahead & call the function that might or might not die of it?


Stop checking for NULL pointers! 


just to be extra-clear, i am not the author of the piece i linked! :^)

i am not sure there's a meaningful difference between dereferencing it and passing it to something else which will.

that said, i actually appreciate a good `assert(ptr);` even if the function is not going to directly use ptr.

ideally, whoever first takes the address of an object should be responsible for ensuring its validity!

@danso ... yeah I started out replying kinda thinking it was yours, but then I did eventually realize it wasn't :D

Sounds like it's three questions, actually:
- should you check validity?
- is NULL valid?
- what do I do if I get something invalid?

... with the main case for assert()-s being: it's useful to crash on invalid stuff, NULLs are _usually_ invalid, and they're the only thing for which you even have a chance telling they're invalid?

(but +1 for "assert()-s are appreciated :))

Sign in to participate in the conversation

Fosstodon is an English speaking Mastodon instance that is open to anyone who is interested in technology; particularly free & open source software.