cdynamic-memory-allocationreallocalloc

Resizing a two dimensional array in C. Memory leak


So I have a two dimensional dynamic array and I have to resize it. After I resize it the compiler says that it can't access the memory trough a format like this: array[i][j]

void resize(int **array,int newsize,int size){
    int newarraysize=WINDOWY/newsize;
    int arraysize=WINDOWY/size;
    array= (int**)realloc(array,(newarraysize*sizeof(int*)));

    if(newarraysize>arraysize){
        for(int i=0; i<arraysize; i++)
         array[i]=(int*)realloc(tomb[i],(newarraysize*sizeof(int)));
    }
    else{
        for(int i=0; i<newarraysize; i++)
         array[i]=(int*)realloc(tomb[i],(newarraysize*sizeof(int)));
    }
    printf("\n");
    for(int i=0;i<newarraysize;i++)
        {
            for(int j=0;j<newarraysize;j++)
            printf("[%d][%d]: %p ",i,j,&array[i][j]);
            printf("\n");
        }
}

Solution

  • If the newsize is smaller, the elements from newsize to size must be freed.
    Then the array can be reallocated.
    If newsize is larger, the elements from size to newsize need to be set to NULL.
    Then reallocate from element 0 to newsize.
    For a pointer to pointer the pointer can be returned and assigned. The other option is to pass a pointer to the pointer int ***array.

    #include <stdio.h>
    #include <stdlib.h>
    
    int **resize ( int **array, int *newsize, int size){
        int **temp = NULL;
    
        if ( *newsize < size) {
            for ( int i = *newsize; i < size; i++) {
                printf ( "free[%d]\n", i);
                free ( array[i]);
            }
        }
    
        if ( NULL == ( temp = realloc ( array, *newsize * sizeof *array))) {
            if ( 0 != *newsize) {
                fprintf ( stderr, "realloc problem\n");
                *newsize = size;
                return array;
            }
            else {
                return NULL;
            }
        }
        array = temp;
    
        for ( int i = size; i < *newsize; i++) {
            array[i] = NULL;
        }
    
        for ( int i = 0; i < *newsize; i++) {
             array[i] = realloc ( array[i], *newsize * sizeof **array);
        }
        printf("\n");
        for ( int i = 0; i < *newsize; i++) {
            for ( int j = 0; j < *newsize; j++) {
                printf ( "[%d][%d]: %p\n", i, j, (void *)&array[i][j]);
            }
        }
        return array;
    }
    
    int main ( void) {
        char line[6] = "";
        int **items = NULL;
        int elements = 0;
        int oldsize = 0;
    
        printf ( "enter a number\n");
        while ( fgets ( line, sizeof line, stdin)) {
            if ( 1 == sscanf ( line, "%d", &elements)) {
                if ( 0 <= elements) {
                    items = resize ( items, &elements, oldsize);
                    oldsize = elements;
                }
                if ( 0 == elements) {
                    free ( items);
                    break;
                }
            }
            else {
                printf ( "enter a number\n");
            }
        }
    
        return 0;
    }