C语言 在 C 中分配矩阵

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2128728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:14:49  来源:igfitidea点击:

allocate matrix in C

cpointersmatrixmalloc

提问by Idan

i want to allocate a matrix.

我想分配一个矩阵。

is this the only option:

这是唯一的选择吗:

int** mat = (int**)malloc(rows * sizeof(int*))

for (int index=0;index<row;++index)
{
    mat[index] = (int*)malloc(col * sizeof(int));
}

回答by jason

Well, you didn't give us a complete implementation. I assume that you meant.

好吧,你没有给我们一个完整的实现。我猜你是这个意思。

int **mat = (int **)malloc(rows * sizeof(int*));
for(int i = 0; i < rows; i++) mat[i] = (int *)malloc(cols * sizeof(int));

Here's another option:

这是另一种选择:

int *mat = (int *)malloc(rows * cols * sizeof(int));

Then, you simulate the matrix using

然后,您使用以下方法模拟矩阵

int offset = i * cols + j;
// now mat[offset] corresponds to m(i, j)

for row-major ordering and

用于行优先排序和

int offset = i + rows * j;
// not mat[offset] corresponds to m(i, j)

for column-major ordering.

用于列优先排序。

One of these two options is actually the preferred way of handling a matrix in C. This is because now the matrix will be stored contiguously in memory and you benefit from locality of reference. Basically, the CPU cache will a lot happier with you.

这两个选项之一实际上是在 C 中处理矩阵的首选方式。这是因为现在矩阵将连续存储在内存中,您可以从引用的局部性中受益。基本上,CPU 缓存会对您更满意。

回答by jamesdlin

The other answers already covered these, but for completeness, the comp.lang.c FAQ has a relevant entry:

其他答案已经涵盖了这些,但为了完整起见,comp.lang.c FAQ 有一个相关条目:

How can I dynamically allocate a multidimensional array?

如何动态分配多维数组?

回答by abhay jain

what you can do is

你能做的是

int (*mat)[col];
mat=(int (*)[col])malloc(sizeof(*mat)*row);

and then use this new matrix as mat[i][j]

然后使用这个新矩阵作为 mat[i][j]

回答by Ana Betts

How about just:

怎么样只是:

int* mat = malloc(rows * columns * sizeof(int));

回答by Stéphan Kochen

You may also use calloc, which will additionally zero initialize the matrix for you. The signature is slightly different:

您也可以使用 calloc,它还会为您零初始化矩阵。签名略有不同:

int *mat = (int *)calloc(rows * cols, sizeof(int));

回答by Matthew Scharley

You cancollapse it to one call to malloc, but if you want to use a 2d array style, you still need the for loop.

可以将其折叠为对 malloc 的一次调用,但是如果您想使用二维数组样式,您仍然需要 for 循环。

int** matrix = (int*)malloc(rows * cols * sizeof(int) + rows * sizeof(int*));

for (int i = 0; i < rows; i++) {
    matrix[i] = matrix + rows * sizeof(int*) + rows * cols * sizeof(int) * i;
}

Untested, but you get the idea. Otherwise, I'd stick with what Jason suggests.

未经测试,但你明白了。否则,我会坚持杰森的建议。

回答by George

For a N-Dimensional array you can do this:

对于 N 维数组,您可以这样做:

int *matrix = malloc(D1 * D2 * .. * Dn * sizeof(int)); // Di = Size of dimension i

To access a array cell with the typical way you can do this:

要使用典型方式访问数组单元格,您可以执行以下操作:

int index = 0;
int curmul = 1;
int i;
int indexes = {I1, I2, ..., In}; // Ii = Index in dimension i

for(i = N-1; i >= 0; i--) {
    index = index + indexes(i) * curmul;
    curmul = curmul * Di;
}

(Note: didnt test now but should work. Translated from my Matlab code, but in Matlab index starts from 1, so i MAY made a mistake (but i dont think so))

(注意:现在没有测试但应该可以工作。从我的 Matlab 代码翻译,但在 Matlab 中索引从 1 开始,所以我可能犯了一个错误(但我不这么认为))

Have fun!

玩得开心!