0

I noticed that G++ compiler does permit initializing an array like this:

vector<vector<string> > partition(string s) {
    const int len = s.size();
    vector<vector<string> > subPalins[len+1] ;
    subPalins[0] = vector<vector<string> >();
    subPalins[0].push_back(vector<string>());
    bool isPalin[len][len];
...
    return subPalins[len];
}

The size of array subPalins is len, which is a constant variable. I remember that it is not allowed to initialize a variable-size array since the size of an array must be determined during compiling time. Though len here is a constant, it is still a variable and I don't understand why this is allowed.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
eaglesky
  • 730
  • 2
  • 13
  • 28
  • 3
    Variable length arrays are allowed in C++ [by extension in gcc](http://stackoverflow.com/questions/21273829/does-int-size-10-yield-a-constant-expression) this would work in standard C++ if [you used a integer literal](http://stackoverflow.com/a/21273849/1708801) the method `size` is not a constexpr function. – Shafik Yaghmour Aug 27 '14 at 15:05
  • 1
    g++ supports C99-style variable-length arrays as an extension. – T.C. Aug 27 '14 at 15:06
  • 2
    `I noticed that G++ compiler does permit initializing an array like this:` If you compiled with `-Wall -pedantic` options, you will see that g++ gives you an error. – PaulMcKenzie Aug 27 '14 at 15:10

0 Answers0