【问题标题】:How to make function to return integer point 2D in C如何使函数在C中返回整数点2D
【发布时间】:2013-11-06 18:38:20
【问题描述】:

我有一个关于返回整数点 2D 的问题。假设我有一个表示二维矩阵的整数点 2D。我想转置矩阵。如果我使用正常方式(返回 int**),我已经成功运行,但问题是 malloc 时无法删除内存。所以我想将此函数转换为使用引用函数作为void transposeMatrix(....)返回void //它将返回矩阵G的转置

int** transposeMatrix(int** G,int  nRowSize,int nColumnSize)
{
    int **GT=NULL;
    int nRowIndex,nColumnIndex;
    GT= (int**)malloc(sizeof(int*) * nRowSize);
    memset(GT, 0, sizeof(int*) * nRowSize);
    for (nRowIndex = 0; nRowIndex < nRowSize; nRowIndex++)
        {
        GT[nRowIndex] = (int*)malloc(sizeof(int) * nColumnSize);
        for (nColumnIndex = 0; nColumnIndex < nColumnSize; nColumnIndex++)
            {
                       GT[nRowIndex][nColumnIndex]=G[nColumnIndex][nRowIndex];
            }
        }
     return  GT;
}

你能帮帮我吗?

【问题讨论】:

  • C++可以使用。但我认为 malloc 和 new 是一样的。所以我标记 C++
  • 你希望它返回G,还是引用G并返回void?
  • 这将是可怕的 C++ 代码。如果您想使用 C++,请将其 only 标记为 C++,您将得到正确的解决方案。
  • 在您的调用程序使用转置矩阵完成之前,您不得删除分配的内存 - 如果您这样做,您将无法使用。结果再说了。您应该编写一个“delete2D”函数,在完成转置后正确撤消内存分配 - 除此之外您很好。
  • 不,在函数外释放内存不是“malloc 问题”。

标签: c


【解决方案1】:

你可以传递一个指向G的指针:

void transposeMatrix(int*** PG,int  nRowSize,int nColumnSize)
}
  ...
  GT[nRowIndex][nColumnIndex]=(*PG)[nColumnIndex][nRowIndex];
  ...
  *GP = GT;
}

或通过引用传递G(我不记得C是否允许这样做):

void transposeMatrix(int** &G,int  nRowSize,int nColumnSize)
}
  ...
  GT[nRowIndex][nColumnIndex]=G[nColumnIndex][nRowIndex];
  ...
  G = GT;
}

【讨论】:

  • 函数中的 GT 是什么?我认为它必须有 4 个参数,例如 void transposeMatrix(int** &GT,int** &G,int nRowSize,int nColumnSize)
  • 你为什么要为已经是指针的东西创建一个指针,这没有任何意义并且是多余的。 G 已经是一个指针。因此让它像 void 'transposeMatrix(int **PG, int nRowSize, int nColumnSize)' 工作得很好
  • @WhozCraig:问题是“我想将此函数转换为返回 void”,但 OP 在其他地方暗示(现在在 cmets 中声明)该函数应返回转置矩阵。我在猜测。而且我知道内存泄漏,我试图一次解决一个问题。
  • @user2938494: GT 在我的功能和你的一样。
  • @user2938494 所以,您知道某人的答案可能有什么问题,但不知道您自己的代码有什么问题? I C ...“所以我想将此函数转换为返回void”。这就是这样做的方法。
【解决方案2】:

好吧,在再次对你的问题有了一些想法之后,因为我非常喜欢这个主题。我有点觉得我找到了一个合适的解决方案。转置函数对矩阵 m 进行更改,使其指向转置矩阵的正确内存地址。因此它释放了先前的矩阵 m 以解决任何内存韭菜问题并更改维度以便在调用者中正确使用。

如果还有部分遗漏,请评论。

我希望这会有所帮助。万事如意

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef int **matrix;

matrix newMatrix(int row, int col)
{
    int idx;
    matrix m = malloc(row * sizeof(int*));
    for (idx = 0; idx < row; ++idx)
    {
        m[idx] = malloc(col * sizeof(int));
    }
    return m;
}

void freeMatrix(matrix m, int row, int col)
{
    int idx;
    for (idx = 0; idx < row; ++idx)
    {
        free(m[idx]);
    }
    free(m);
}

void printMatrix(matrix m, int row, int col)
{
    int r, c;
    for (r = 0; r < row; r++)
    {
        for (c = 0; c < col; c++)
        {
            printf("%d ", m[r][c]);
        }
        printf("\n");
    }
}

void swap(int *a, int *b)
{
    int h = *a;
    *a = *b;
    *b = h;
}

void transpose(matrix *m, int *row, int *col)
{
    int r, c;
    matrix t = newMatrix(*col, *row);

    for (r = 0; r < *row; ++r)
    {
        for (c = 0; c < *col; ++c)
        {
            t[c][r] = (*m)[r][c];
        }
    }
    freeMatrix(*m, *row, *col);
    swap(row, col); 
    (*m) = t;
}

int main()
{
    int row = 2, col = 3;
    matrix m = newMatrix(row, col);

    m[0][0] = 1;
    m[0][1] = 1;
    m[0][2] = 1;
    m[1][0] = 2;
    m[1][1] = 4;
    m[1][2] = 3;

    printMatrix(m, row, col);
    transpose(&m, &row, &col);
    printMatrix(m, row, col);       

    return 0;
}

【讨论】:

  • 你测试了吗?我已经检查了它和一些 cmets: 1:当你使用 malloc 函数时,你必须在它旁边分配 int* 或 int** 。第二:freeMatrix 没用。我必须删除这个函数,结果会正确。但是在使用的时候。程序崩溃了。我正在使用 Visual Studio 2010。感谢 Montaldo。
  • 我已经测试过了,它可以在 mac、linix 和在线编译器上运行。 malloc 函数的强制转换不是必需的。严格来说 malloc 返回一个 void*,但它是不必要的,因为这个 void * 会自动且安全地提升为任何其他指针类型。因此,它使您的代码可读性降低并且更容易出错。例如,如果您忘记了 stdlib 并且可能会崩溃,它会隐藏错误
  • 我在 Visual Studio 中对其进行了测试,发现您在 free m 矩阵中可能错了。当您调用“矩阵 t = newMatrix(*col, *row);”时然后将 t 行的值设置在 m 的前面 col;并且行的当前值为3。(作为您的示例)。因此您调用 freeMatrix(*m,*row, *col);这意味着您将释放行等于 3 和行等于 2 的矩阵 m,这是错误的。所以您必须调用矩阵 t = newMatrix(*col, *row);你的想法和我一样吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-03
  • 1970-01-01
相关资源
最近更新 更多