What is the best way to free memory after returning from an error?

后端 未结 9 2256
一整个雨季
一整个雨季 2020-12-13 22:00

Suppose I have a function that allocates memory for the caller:

int func(void **mem1, void **mem2) {
    *mem1 = malloc(SIZE);
    if (!*mem1) return 1;

            


        
9条回答
  •  感情败类
    2020-12-13 22:29

    This is where a goto is appropriate, in my opinion. I used to follow the anti-goto dogma, but I changed that when it was pointed out to me that do { ... } while (0); compiles to the same code, but isn't as easy to read. Just follow the some basic rules, like not going backwards with them, keeping them to a minimum, only using them for error conditions, etc...

    int func(void **mem1, void **mem2)
    {
        *mem1 = NULL;
        *mem2 = NULL;
    
        *mem1 = malloc(SIZE);
        if(!*mem1)
            goto err;
    
        *mem2 = malloc(SIZE);
        if(!*mem2)
            goto err;
    
        return 0;
    err:
        if(*mem1)
            free(*mem1);
        if(*mem2)
            free(*mem2);
    
        *mem1 = *mem2 = NULL;
    
        return 1;
    }
    

提交回复
热议问题