In C, I am calling many functions having to check if they worked. The code gets ~3x bigger because of it! An example:
char *myStr = malloc(sizeof(char));
if (!myStr)
return NULL;
Is there a more readable way to code calling and error-checking in C? Or is this just the way the cookie crumbles?
I have tried the following macro. But it does not seem idiomatic in C, nor it worked -- the mistake was pointed by "Key Y-N".
#define TRY_EXCEPT(try_statement, except_condition, except_statement) try_statement##if(except_condition){except_statement}
Edit: This is the density of calling and error-checking I meant.
if (!varNames)
goto exit_fail;
struct dynStr *asmCode = dynStr_create();
if (!asmCode)
goto exit_fail;
int rc;
rc = dynStr_append(asmCode, ".data\n");
if (!rc)
goto exit_fail;
char *varName;
for (size_t i = 0; (varName = daStr_getString(varNames, i)); i++) {
rc = dynStr_append(asmCode, "DD ");
if (!rc)
goto free_and_exit;
rc = dynStr_append(asmCode, varName);
if (!rc)
goto free_and_exit;
rc = dynStr_append(asmCode, "\n");
if (!rc)
goto free_and_exit;
}
I'd condense it like so:
Or use another variable:
Or simply remove the assignment to
rc: