【发布时间】:2019-11-09 07:40:12
【问题描述】:
这是这个问题的后续问题:C - Convert array of elements into 2-d matrix
要进行转换,我得到的方法是:
int (* M)[b] = (int (*) [b])array
M[a][b] 和 array[a * b] 的位置。
问题是我发现当我以这种方式进行转换时,实际上在堆栈上分配了一个大小为b 的数组,对于一个大的b 来说有点昂贵。所以我想知道是否有任何方法可以在没有这种分配的情况下进行转换。
示例(更新):
/* conversion.c */
int main(void) {
int flag, array[30], b = 3;
if (flag)
goto done;
int (* M)[b] = (int (*)[b]) array;
done:
return 0;
}
用 gcc-9 编译:
conversion.c: In function 'main':
conversion.c:5:3: error: jump into scope of identifier with variably modified type
5 | goto done;
| ^~~~
conversion.c:9:1: note: label 'done' defined here
9 | done:
| ^~~~
conversion.c:7:9: note: 'M' declared here
7 | int (* M)[b] = (int (*)[b]) array;
| ^
conversion.c:5:3: error: jump into scope of identifier with variably modified type
5 | goto done;
| ^~~~
conversion.c:9:1: note: label 'done' defined here
9 | done:
| ^~~~
conversion.c:7:9: note: '({anonymous})' declared here
7 | int (* M)[b] = (int (*)[b]) array;
| ^
更新:
error: jump into scope of identifier with variably 表示 goto 语句正在尝试跳过(编译时)堆栈上未知大小的内存,这意味着由于上述转换,堆栈上分配了内存。
【问题讨论】:
-
here is actually an array of size b allocating on the stack你怎么确定这是因为int (*M)[b]而发生的。你能显示minimal reproducible example吗? -
如果没有minimal reproducible example,我们不知道您指的是什么
goto语句和编译错误。帖子中的代码不会占用我们可以说的堆栈内存。 -
“实际上有一个大小为 b 的数组在堆栈上分配”谁说的? M 是指向大小为 b 的数组的指针。但是,您的演员表是非标准的,并且在取消引用 M 时可能会导致 UB。并且请不要使用 goto,这里完全没有根据。
-
不,编译器错误并没有说明内存、堆栈或任何此类废话。它谈到了标识符和类型。
-
投票结束不清楚,因为问题现在提出了完全不同的问题,并且发布的答案不再有意义。这是浪费大家的时间,干脆关闭吧。
标签: c arrays pointers matrix type-conversion