I have taken as a practice (as many others, I think) to always do this when working with pointers:

void I_take_pointer(some_type* arg1)
        //safely do stuff with pointer
    //optionally an else statement

...in almost all functions, except those where I can be sure arg1 will never be null.

Is it good practice/should I perform this check on the SDL_Event* I get on input when working with SDL2?

  • \$\begingroup\$ I'd think if the function is always expecting a valid SDL_Event then it's probably overkill. \$\endgroup\$ Nov 4, 2014 at 19:32
  • 2
    \$\begingroup\$ An argument in favour of sanity-checking in general: The check serves as documentation that a null-value is unexpected here, which might make your code easier to read later and make bugs totally obvious. You could wrap the check as an assert or #ifdef, to exclude it from a production build. \$\endgroup\$
    – Anko
    Nov 4, 2014 at 19:39
  • 1
    \$\begingroup\$ Test the pointer with an if when the argument is optional. assert when the argument must not be null. \$\endgroup\$
    – glampert
    Nov 5, 2014 at 1:13

1 Answer 1


You do not need to test for nullness in this case.

The reason is that the ultimate source of the object is your code.

SDL_Event e;

Both ways of getting events from SDL demand that you pass in the event object to be filled, so it can thus not be NULL unless you fail to allocate one beforehand.

  • \$\begingroup\$ It was over 9 months since I wrote that code in this project... Totally forgot about it ;) \$\endgroup\$
    – Ludwik
    Nov 4, 2014 at 19:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .