在二维数组c使用的realloc [英] Using realloc on a 2D array c

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

问题描述

我有点新的C,对不起,如果我的问题是有些模糊;

I'm kinda new to C sorry if my questions is somewhat vague;

我需要一个二维数组上使用的realloc不失它的previous数据,我有这个功能在我的程序做到这一点:

I need to use realloc on a 2D array without losing it's previous data, I have this function in my program to do it:

void modifyMatrix(int **iMat, int iRow, int iRow2, int iCol)
{
    int i;
    iMat = (int**)realloc(iMat, (iRow2)*sizeof(int*));
    for(i=iRow; i<iRow2; i++)
    {
        iMat[i]=NULL;
    }
    for(i=0; i<iRow2; i++)
    {
        iMat[i]=(int*)realloc(iMat[i], (iCol)*sizeof(int));
    }
}

其中iRow是原来的大小和iRow 2及ICOL是新的大小和都正在计划在其他地方拍摄的。

Where iRow is the original size and iRow 2 & iCol are the new size and are all being captured elsewhere in the program.

每当我尝试打印我不断收到的行和列的添加,我究竟做错了垃圾数据或存储值矩阵?

Whenever I try to print the matrix I keep getting junk data or memory values on the rows and columns that are added, what am I doing wrong?

让我知道如果你需要完整的code或任何其他问题的澄清,先谢谢了!

Let me know if you need the full code or any other questions to clarify, thanks in advance!

编辑:
下面你可以看到code我用它来创建矩阵

Below you can see the code I use to create the Matrix

我的坏,我想我应该补充说,该矩阵已经在程序的其他地方创建的,这个功能我只是想修改尺寸,感谢您的快速反应BTW!下面你可以找到与我创建阵列功能

My bad, I think I should've added that the Matrix is already created elsewhere in the program, with this function I'm just trying to modify the dimensions, thanks for the quick response btw!, below you can find the function with which I'm creating the array

void createMatrix(int ***iMat, int iRow, int iCol)
{
    int **iRow2 = (int**)calloc(iRow, sizeof(int*));
    int i;
    for (i=0; i<iRow; i++)
    {
        iRow2[i] = (int*)calloc(iCol, sizeof(int));
    }
    *iMat=iRow2;
}

另外,我只能用我已经创建了这样做的阵列,我不能创建一个临时一(我知道会是简单的方法来做到这一点)

Also, I can only use the array I've already created to do this, I can't create an temp one (which I know would be the easy way to do it)

推荐答案

在成功,的realloc 释放你传递给它的指针,并返回一个指向新分配的内存。你得到的垃圾值因为访问的指针,释放的内存是不确定的行为。

On success, realloc frees the pointer you pass to it and returns a pointer to the newly allocated memory. You're getting "junk values" because dereferencing a pointer to freed memory is undefined behavior.

这不是pretty,但解决这一问题的方式(如另一个答案指出)是通过三指针( INT *** )。这样的功能可以修改指针的原始值。这是你如何模拟C,这是一个严格的语言按值传递引用语义。

It's not pretty, but the way to fix this (as another answer pointed out) is to pass a triple pointer (int***). That way the function can modify the original value of the pointer. This is how you simulate reference semantics in C, which is a strictly "pass by value" language.

void modifyMatrix(int ***iMat, int iRow, int iRow2, int iCol)
{
    int i;
    int **newMatrix = realloc(*iMat, iRow2 * sizeof(int*));
    if (newMatrix == NULL) {
           /* handle realloc error here */
    }
    else {
          *iMat = newMatrix; /* assign pointer to the new memory */
    }

    for(i=iRow; i<iRow2; i++)
    {
         (*iMat)[i]=NULL;
    }

    for(i = 0; i < iRow2; i++)
    {
        int* newRow = realloc((*iMat)[i], (iCol)*sizeof(int));
        if (newRow == NULL) {
              /* handle realloc error here */
        }
        else {
              (*iMat)[i] = newRow;
        }
    }
}

你还要增加一些错误检查。如果realloc的失败,返回NULL指针,你刚刚创建了内存泄漏:你不再有指针的previous值

You've also got to add some error checking. If realloc fails and returns a NULL pointer, you've just created a memory leak: you no longer have the previous value of the pointer.

请注意,我删除了所有的类型转换。这是不好的做法投malloc的返回和朋友的C.

Note that I removed all of your casts. It's bad practice to cast the return of malloc and friends in C.

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

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