1

I want to reallocate a 2d array, so that the arrays in the second array become bigger, so the things I want to store are bigger than the arrays I want to store them in and I want to make the arrays bigger. The problem is that I do not really know how to do this. I got it to compile without errors, but in Valgrind I saw a lot of memory errors, so I do something wrong. I saw a previous question about this here but I do not really understand it, so any help and explanation on how to do this would be greatly appreciated.

I have this so far.

int **create2darray(int a, int b) {
  int i;
  int **array;

  array = malloc(a * sizeof(int *));
  assert(array != NULL);
  for (i = 0; i < a; i++) {
    array[i] = calloc(b, sizeof(int));
    assert(array[i] != NULL);
  }
  return array;
}

int **reallocArray(int **array, int size, int i) {
  int i;
  int **safe_array;
  safe_array = realloc(*array ,2 * size);
  assert(safe_array != NULL);
  array = safe_array;
  return array;
}

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


int main(int argv, char *argc[]) {
  int i;
  int size;
  int **testArray = create2darray(1, 10);
  size = 10;
  for(i = 0; i < size; i++) {
    testArray[0][i] = 2;
  }
  testArray[0] = reallocArray(testArray, size, 0);
  size = 2 * size;
  for(i = 9; i < size; i++) {
    testArray[0][i] = 3;
  }
  for(i = 0; i < size; i++) {
    printf("%d", testArray[0][i]);
    free2DArray(testArray, size);
  }
  return 0;
}
5gon12eder
  • 24,280
  • 5
  • 45
  • 92
Rub3n
  • 31
  • 3
  • 2
    What errors are you getting? – Scott Hunter Feb 03 '16 at 19:39
  • It's not quite clear what you want to re-allocate or what the last parameter to `reallocArray` is. At present, you only re-allocate the first row. When you want your whole array to grow, you must do a two-level re-allocation just as in `createArray`. – M Oehm Feb 03 '16 at 19:50
  • Are you trying to say that you want to increase the number of columns in the array? – M.M Feb 04 '16 at 00:15

2 Answers2

0

In Free2DArray(), you free() the individual arrays of integers, but not the "outer" dimension of the array which holds the integer pointers. You could add another call to free() after the loop to take care of that.

In main():

for(i = 0; i <size; i++) {
    printf("%d", testArray[0][i]);
    free2DArray(testArray, size);
}

you will end up free()-ing the (inner) integer arrays multiple times. The call to free2DArray() should be outside the loop.

Jim Lewis
  • 43,505
  • 7
  • 82
  • 96
0

You need a function reallocArray which realaoctes the outer array and all the inner arrays too. Adapt youre code like this:

#include <malloc.h> 

int **reallocArray( int **array, int oldSizeA, int newSizeA, int newSizeB )
{
    // realloc the array of pointers ( allocates new memory if array == NULL )
    int **safe_array = realloc( array, newSizeA * sizeof( int* ) );
    assert(safe_array != NULL);
    if ( safe_array == NULL )
        return array;
    array = safe_array;

    // realloc the inner arrays of int ( allocates new memory if i >= oldSizeA )
    for ( int i = 0; i < newSizeA; i ++ )
    {
        int *temp = NULL;    // allocate new memory if i >= oldSizeA
        if ( i < oldSizeA )  
            temp = array[i]; // reallocate array[i] if i < oldSizeA

        temp = realloc( temp, newSizeB * sizeof( int ) );
        assert( temp != NULL );
        if ( temp == NULL )
            return array;
        array[i] = temp;
    }
    return array;
}

Use function reallocArray in your function create2darray to create your array. If the input paramter of ralloc is NULL, then new dynamic memory is allocated.

int **create2darray( int sizeA, int sizeB )
{
    return reallocArray( NULL, 0, sizeA, sizeB );
}

First you have to free the inner arrays of int in a loop, then you have to free the array of pointers:

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


int main( int argv, char *argc[] ){

    int sizeA = 1;
    int sizeB = 10;
    int **testArray = create2darray( sizeA, sizeB );
    for ( int i = 0; i < sizeB; i++ ) {
        testArray[0][i] = 2; 
    }

    int oldSizeA = sizeA;
    int oldSizeB = sizeB;
    sizeB = 2*sizeB;
    testArray = reallocArray( testArray, oldSizeA, sizeA, sizeB );
    for( int i = oldSizeB; i < sizeB; i++ ) {
        testArray[0][i] = 3;
    }

    for( int i = 0; i < sizeB; i++ ) {
        printf("%d", testArray[0][i]);
    }

    free2DArray(testArray, sizeA );
    return 0;
}
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • I forgot the second free. I fixed that now, but I do not understand the last if-statement. The if-statement never works right?. if temp is idd NULL then the assert will interupt, but the code itself is really nice. I did not know that the pointers had to be safe temporary, so thanks :D `(temp = realloc( temp, newSizeB * sizeof( int ) ); assert( temp != NULL ); if ( temp == NULL ) return array; array[i] = temp; } return array; }` – Rub3n Feb 04 '16 at 21:39