Is it safe to use void ** when freeing double pointers of other types?

I created a two-dimensional array in a double pointer int with the following methodology.

int **make2Dint(int rows, int columns) {
    //a is our 2D array
    int **a = (int **)calloc(rows, sizeof(int *));
    //If we can't allocate the necessary memory, return null.
    if (!a) {
        return (int **)NULL;
    }

    //Allocate each row with the correct amount of column memory.
    for (int i = 0; i < rows; i++) {
        a[i] = (int *) calloc(columns, sizeof(int));
        //If we can't allocate the necessary memory, return null.
        if (!a[i]) {
            return (int **)NULL;
        }
    }

    return a;
}

For this, I also wrote a free function that looks like this

void free2DArray(void **a, int rows) {
    for (int i = 0; i < rows; i++) {
        free(a[i]);
    }

    free (a);
}

When you are going to use my free function (however free2DArray(array, rows);, gcc gave me a warning.

In file included from life.c:6:0:
twoD.h:14:6: note: expected ‘void **’ but argument is of type ‘int **’
 void free2DArray(void **a, int rows);
      ^~~~~~~~~~~

Now I can get away with the act in void **, but this seems to indicate that my use is void **problematic.

C. void * , ( ) , void *; , void **, , void *. void ** (, * void *, void **), , void * . , , *; .

, void **, , - void *; (void **) dp, , ( , , . 13.9). , void **, void *, , void *, .

, , , . - , ?

+4
2

. , C , , (), . , , , ( ), "" 2D-

+2

2D- 1D - . , free() - .

, ( ) 2D-, , /.

+1

Source: https://habr.com/ru/post/1692796/


All Articles