4 ms·
void * * a = NULL; while(TRUE) { a = malloc(sizeof *a); if(a == NULL) break; *a = malloc(1024); if(*a == NULL) break;
by quelsolaar 6y ago
void * * a = NULL;
while(TRUE)
{
a = malloc(sizeof *a);
if(a == NULL)
break;
*a = malloc(1024);
if(*a == NULL)
break;
if(!do_some_other_tests(a))
break;
return a;
}
/* cleanup /
if(a == NULL)
return;
if(a != NULL)
free(*a);
free(a);
alt:
void * * a = NULL;
switch(TRUE)
{
defaultt :
a = malloc(sizeof *a);
if(a == NULL)
break;
*a = malloc(1024);
if(*a == NULL)
break;
if(!do_some_other_tests(a))
break;
return a;
}
/* cleanup /
if(a == NULL)
return;
if(a != NULL)
free(*a);
free(a);
- grok22 6y agoIf the proposed implementation requires a guard clause, then it seems like it is no better than the while (TRUE) implementation above and it's not worth the support in the standard. Maybe it's a bit more structured and covers a few more use-cases, but not worth the standardization.
- gusttedt 6y agoIn the current proposal, the whole function body can act as a guarded block. So for simple cleanup strategies that are bound to functions, you wouldn't need this.