News for hackers. Stuff that matters.
Re:Tips and Tricks to avoid memory leaks in writing C by JoachimSchipper on Wednesday June 15, 2011 @07:22AM (#2656628)
"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;
Re:Tips and Tricks to avoid memory leaks in writing C by naughtysriram on Wednesday June 15, 2011 @09:10AM (#2656902)
Nice 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");} }