Search code examples
c++arrayssegmentation-faulticc

ICC segfaulting with variable length arrays


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


Solution

  • 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.