C 中的可变大小矩阵
有没有办法在 C (不是 C++,只是 C)中创建可变大小的双脚本数组?我知道要创建一个可变大小的单脚本数组,您只需使用一个指针,例如
float *array;
array = (float *) calloc(sizeof(float), n);
创建一个大小为 n 的浮点数的单脚本数组。我可以为双脚本数组做类似的事情吗?
Is there any way to create a variable sized doubly-scripted array in C (not C++, just C)? I know that to create a variable sized singly-scripted array, you just use a pointer, e.g.
float *array;
array = (float *) calloc(sizeof(float), n);
creates a singly-scripted array of floats of size n. Is there something similar that I can do for doubly-scripted arrays?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我很惊讶没有人指出“明显”的替代方案,它保留主矩阵的单个连续分配,但具有给出双下标的指针向量。 (我想这意味着它并不明显。)
现在您可以像往常一样编写二维数组访问:
显然,我们还需要检查内存分配。
I'm surprised no-one has pointed out the 'obvious' alternative which preserves the single contiguous allocation for the main matrix, but has the vector of pointers to give double subscripting. (I suppose that means it isn't obvious, after all.)
Now you can write 2-D array accesses as usual:
Clearly, we also need to check the memory allocation.
geeksforgeeks.com 中给出了一种更简单的方法“使用指向 VLA 第一行的指针”:
然后您可以像这样简单地访问每个元素
There is a simpler way given in geeksforgeeks.com "Using a pointer to the first row of VLA":
and then you can access each element simply like
C 中没有双脚本数组;只有数组的数组。例如:
应该读作“3 个整数数组的数组”,而不是“3x3 整数数组”。这从表达式的类型中可以立即看出 - 例如,
a[0]
是一个有效的表达式,其类型为int[3]
。对于数组类型,数组大小是类型的一部分,因此必须在编译时知道。因此,虽然您可以使用“指向数组的指针”类型来使一维动态化,但仍然需要修复其余的维度:
有两种传统的解决方法:
只需使用 的单维动态数组width x height 元素,并根据 2D 坐标计算 1D 索引,如
(y * width + x)
。使用指针到指针:
这里的问题是你的数组不再需要是矩形的,而且你不能真正强制它。从性能角度来看,它也比单个连续的内存块更差。
在C99中,还可以使用变长数组:
There are no double-scripted arrays in C; there are only arrays of arrays. E.g. this:
Should read as "array of 3 arrays of 3 ints", not as "array of 3x3 ints". This is immediately visible from types of expressions - e.g.
a[0]
is a valid expression, and its type isint[3]
.For array types, array size is part of the type, and therefore must be known at compile-time. Therefore, while you can have a type "pointer to arrays" to make one dimension dynamic, the remaining ones would still have to be fixed:
There are two traditional workarounds:
Just use a single-dimensional dynamic array of width x height elements, and calculate 1D indices from 2D coordinates as
(y * width + x)
yourself.Use pointer to pointers:
The problem here is that your array needs not be rectangular anymore, and you can't really enforce it. Performance-wise, it's also worse than a single contiguous block of memory.
In C99, you can also use variable-length arrays:
comp.lang.c FAQ对此有很好的部分。
The comp.lang.c FAQ has a good section on this.
对于多维数组,您几乎可以做同样的事情。
You can do almost the same thing for multi dimensional arrays.
如果你想要一个 n 行 m 列的矩阵,那么你可以使用长度
m*n
的线性数组来表示它,其中每个索引i
表示和逆映射
大多数使用矩阵(如 matlab)的代数包实际上都使用这种表示形式,因为它可以很好地推广到任何维度(您也可以将其推广到 3 维矩阵)。
If you want a matrix with n rows and m columns then you can use a linear array of length
m*n
to represent this, where each indexi
representsand the inverse mapping
Most algebra packages that use matrices like matlab actually use this representation, because it generalizes well to any dimension (you could generalize this to a 3-dimensional matrix just as well).
不,那不可能。作为替代方案,分配一个数组,并定义一个索引函数,该函数获取坐标并返回数组的索引。
No, that's not possible. As an alternative, allocate a single array, and define an indexing function that takes your coordinate and returns an index into the array.
您可以使用 C99 可变长度数组(与 gcc 一起使用):
You can use C99 variable-length arrays (works with gcc):