可变长度数组的 ICC 段错误
因此,当使用基本的 icc bob.cpp -o bob
编译并运行时,以下代码会出现段错误:
#include <string>
int foo () {
return 6;
}
int main() {
std::string t[foo()];
}
但是,以下两个类似的程序似乎运行良好。
#include <string>
int foo () {
return 6;
}
int main() {
int f = foo();
std::string t[f];
}
我
#include <string>
int foo () {
return 6;
}
int main() {
std::string t[6];
}
对发生的事情有点困惑。显然,可变长度数组是非标准的,这让我感到惊讶,因为我一直使用支持它的 g++。但是,如果ICC不支持,为什么还能编译呢?另外,为什么示例 2 会“起作用”?
这里的正确代码是什么?如果第一个代码片段不正确,为什么它会编译,然后为什么会出现段错误?
我在 2011 x86_64 Intel(R) Core(TM) i5 上使用 icc (ICC) 12.0.2 20110112。
谢谢
So, when compiled with the basic icc bob.cpp -o bob
and run, the following code segfaults:
#include <string>
int foo () {
return 6;
}
int main() {
std::string t[foo()];
}
The following two similar programs, however, seem to run fine.
#include <string>
int foo () {
return 6;
}
int main() {
int f = foo();
std::string t[f];
}
and
#include <string>
int foo () {
return 6;
}
int main() {
std::string t[6];
}
I'm a bit confused about what's going on. Apparently, variable length arrays are non-standard, and this was a surprise to me since I've always used g++ which supports it. However, if it's not supported by ICC, why would it compile? Also, why would example 2 "work"?
What is correct code here, and, if the first snippet is incorrect, why does it compile, and then why does it segfault?
I'm using icc (ICC) 12.0.2 20110112 on 2011 x86_64 Intel(R) Core(TM) i5.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,虽然 C++ 确实没有可变长度数组(尽管 C99 确实如此),但显然 ICC 确实支持它们作为扩展,因为您的代码实际上编译(并且因为您的第二个代码段实际上运行时没有崩溃)。
如果第一个版本崩溃,那么它一定是 ICC 的非标准扩展实现中的错误。
Well, while it is true that C++ has no variable-length arrays (C99 does though), apparently ICC does support them as an extension, since your code actually compiles (and since your second snippet actually runs without crashing).
If the first version crashes, then it must be a bug in ICC's implementation of that non-standard extension.