在C二维数组重新分配 [英] reallocation of 2d arrays in c

查看:251
本文介绍了在C二维数组重新分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要重新分配一个二维数组,使第二数组中的数组变得更大,所以我想存储的东西比我想将它们存储在阵列大,我想使阵列更大。问题是,我真的不知道如何做到这一点。我懂了没有错误编译,但在Valgrind的我看到了很多的内存错误,所以我做了什么。只见这个这里previous问题但我没有真正了解它,所以任何帮助,并解释如何做到这将是极大的AP preciated。

我有这这么远。<​​/ P>

  INT ** create2darray(INT A,INT B){
  INT I;
  INT **阵列;  阵列=的malloc(A *的sizeof(INT *));
  断言(阵!= NULL);
  对于(i = 0; I&LT; A;我++){
    数组[我] =释放calloc(B,的sizeof(INT));
    断言(数组[I]!= NULL);
  }
  返回数组;
}INT ** reallocArray(INT **阵列,诠释大小,int i)以{
  INT I;
  INT ** safe_array;
  safe_array = realloc的(*阵列,2 *的大小);
  断言(!safe_array = NULL);
  阵列= safe_array;
  返回数组;
}无效free2DArray(INT **阵列,诠释米){
  INT I;
  对于(i = 0; I&LT;米;我++){
    免费(数组[我]);
  }
}
INT主(INT argv的,字符*的argc []){
  INT I;
  INT大小;
  INT ** testArray = create2darray(1,10);
  大小= 10;
  对于(i = 0; I&LT;大小;我++){
    testArray [0] [I] = 2;
  }
  testArray [0] = reallocArray(testArray,大小,0);
  大小= 2 *的大小;
  对于(i = 9; I&LT;大小;我++){
    testArray [0] [I] = 3;
  }
  对于(i = 0; I&LT;大小;我++){
    的printf(%D,testArray [0] [I]);
    free2DArray(testArray,大小);
  }
  返回0;
}


解决方案

您需要一个函数 reallocArray 这realaoctes外部阵列和所有内部阵列了。
适应您选择code是这样的:

 的#include&LT;&malloc.h所GT;INT ** reallocArray(INT **阵列,诠释oldSizeA,诠释newSizeA,诠释newSizeB)
{
    // realloc的指针数组(如果数组分配新的内存== NULL)
    INT ** safe_array = realloc的(数组,newSizeA * sizeof的为(int *));
    断言(!safe_array = NULL);
    如果(safe_array == NULL)
        返回数组;
    阵列= safe_array;    // INT的内部数组的realloc(分配新的内存,如果我&GT; = oldSizeA)
    的for(int i = 0; I&LT; newSizeA;我++)
    {
        为int * TEMP = NULL; //分配新的内存,如果我&GT; = oldSizeA
        如果(ⅰ&下; oldSizeA)
            TEMP =阵列[我]如果我和LT //重新分配阵列[我] oldSizeA        TEMP = realloc的(温度,newSizeB *的sizeof(INT));
        断言(温度!= NULL);
        如果(临时== NULL)
            返回数组;
        数组[我] =温度;
    }
    返回数组;
}

使用功能 reallocArray 在函数 create2darray 以创建阵列。如果输入放慢参数 ralloc NULL ,那么新的动态内存分配。

  INT ** create2darray(INT SIZEA,诠释sizeB)
{
    返回reallocArray(NULL,0,SIZEA,sizeB);
}

首先,你必须免费为int在一个循环内的数组,那么你必须免费数组指针:

 无效free2DArray(INT **阵列,诠释SIZEA)
{
    的for(int i = 0; I&LT; SIZEA;我++)
       免费(数组[我]);
    免费(数组);
}
INT主(INT argv的,字符*的argc []){    INT SIZEA = 1;
    INT sizeB = 10;
    INT ** testArray = create2darray(SIZEA,sizeB);
    的for(int i = 0; I&LT; sizeB;我++){
        testArray [0] [I] = 2;
    }    INT oldSizeA = SIZEA;
    INT oldSizeB = sizeB;
    sizeB = 2 * sizeB;
    testArray = reallocArray(testArray,oldSizeA,SIZEA,sizeB);
    的for(int i = oldSizeB; I&LT; sizeB;我++){
        testArray [0] [I] = 3;
    }    的for(int i = 0; I&LT; sizeB;我++){
        的printf(%D,testArray [0] [I]);
    }    free2DArray(testArray,SIZEA);
    返回0;
}

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;
}

解决方案

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;
}

这篇关于在C二维数组重新分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆