instead of checking every input pointer argument with if condition if( in != NULL ), is there better way to do this. One method is to create some macros, like
#define CHECK_ARG(x) if(!(x)) { return 0; }
In code it’s look like this
int some_func( int * first_arg, int * second_arg )
{
CHECK_ARG( first_arg );
CHECK_ARG( second_arg );
// some operations
//
}
In my opinion this is good approach, but before adding this, I want to know is there any better ways doing this?
Thanks in advance!
In your function write
Define assert_or_return so that in the debug version it will fire up the debugger (by calling abort), and in the release version it will log the error and return (as you want to do). I assume you’re confident that returning from the function when one of the arguments is NULL will do more good than harm in the production version of your application. If by returning early you will irrevocably corrupt your users’ data or cause a furnace to explode, it would be better to terminate early, perhaps with a custom error message.
The following example illustrates how your idea can be implemented.