3 ms·
"Exceptions" can be useful: lots of my code looks like int fd = -1; char *buf = NULL if ((fd = open("/foo", O_RDONLY, 0)) == -1) goto err;
by JoachimSchipper 15y ago
"Exceptions" can be useful: lots of my code looks like
int fd = -1;
char *buf = NULL
if ((fd = open("/foo", O_RDONLY, 0)) == -1)
goto err;
if ((buf = malloc(1024)) == NULL)
goto err;
...
return 0;
err:
if (fd =! -1)
close(fd);
free(buf);
return -1;
- naughtysriram 15y agoNice idea, but people would scream seeing those goto's. I used to do those in a macro/function like, #define memalloc(x,y) { x=0;if(x=malloc(y)){}else{printf("error:no mem");} }