Search code examples
cmallocfreecalloc

Freeing a multidimensional array when allocation fails


Say I allocate a two-dimensional array:

int main(void)
{
        long int **arr;
        int i;
        int j;

        arr = calloc(2, sizeof(long int *));
        if (!arr) {
                exit(EXIT_FAILURE);
        }

        for (i = 0; i < 2; i++) {
                arr[i] = calloc(10, sizeof(long int));
                if (!arr[i]) {
                        for (j = i; j > 0; j--) {
                                free(arr[j]);
                        }
                        free(arr);
                        exit(EXIT_FAILURE);
                }
        }
}

Should I include the free()-loop in the test condition if memory allocation fails

if (!arr[i]) {
        for (j = i; j > 0; j--) {
                free(arr[j]);
        }
        free(arr);
        exit(EXIT_FAILURE);
}

or is it usually enough to just exit() with failure when I don't want the program to move on?

if (!arr[i]) {
        exit(EXIT_FAILURE);
}

Solution

  • Since you are exiting the program in case of allocation failure, therefore no harm in doing this. You can use second snippet.


    C does not provide direct support for error handling, aka exception handling. On MSVC you can try this (Note that this is not the part of C standard):

    void exceptionExample() {
        int *p = NULL;
        __try {
            p = (int*)malloc(sizeof(int));
            *p = 5;
            printf("%d\n",*p);
        }
        __finally {
            free(p);
        }
    }  
    

    Suggested readings: C Programming/Error handling, Error handling in C.