-1

I know that in C99 you can declare a array which size is the value of a parameter, like this:

void foo(int size) {
  int array[size];
  // do stuff
}

From what I understand, it is allocated on the stack when the function is called. Apparently this is not supposed to works in C++.

Recently I wrote something like this in C++:

void bar(vector<Thing> vec) {
  for (Thing t : vec) {
    int n = t.get_n();
    int array[n];
    // do stuff
  }
}

Surprisingly the compiler (G++) did not complain, why? Is there an hidden memory allocation there?

dblouis
  • 568
  • 1
  • 5
  • 18
  • 5
    Because you didn't turn on your warnings, and it is a gcc extension – Passer By Nov 21 '17 at 17:06
  • 2
    duplicate of [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) or https://stackoverflow.com/questions/7812566/why-no-variable-size-array-in-stack or https://stackoverflow.com/questions/17899274/g-variable-size-array-no-warning or https://stackoverflow.com/questions/43857625/variable-sized-array-on-the-stack or etc... – underscore_d Nov 21 '17 at 17:07

2 Answers2

4

Variable length arrays are not part of C++ standard. It is an extension provided by G++.

If you will compile it with -pedantic flag then compiler will raise a warning.

haccks
  • 104,019
  • 25
  • 176
  • 264
3

Gcc allows Variable Length Arrays (aka VLAs) in C++ by default even though it is not standard conforming.

If you tell it to strictly adhere to the standard by passing (for example) the -std=c++14 option, then it will correctly reject your code.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
  • I used cmake to build the project where the example come from, with the option `set (CMAKE_CXX_STANDARD 11)`. This is supposed to pass `-std=c++11` to the compiler but according to what you say it did not, or else it would have complain about the array. – dblouis Nov 21 '17 at 17:11
  • 3
    No: https://cmake.org/cmake/help/v3.1/prop_tgt/CXX_STANDARD.html => _"For some compilers, this results in adding a flag such as `-std=gnu++11` to the compile line."_ Therefore it may allow GNU extensions, silently. Stupid yes, undocumented no. – underscore_d Nov 21 '17 at 17:14
  • @DBLouis I'm guessing (you should check), but it probably passed gnu++11 rather than c++11 which allows the GNU extensions. – Jesper Juhl Nov 21 '17 at 17:15
  • I did not thought about that, thank you – dblouis Nov 21 '17 at 17:16
  • Is the array allocated on the heap or on the stack in my second example? – dblouis Nov 21 '17 at 17:33
  • 2
    @DBLouis "Is the array allocated on the heap or on the stack in my second example?" - the standard contains no language such as "heap" or "stack". It talks only in terms of "automatic storage" or "dynamic storage". But even when interpreting that as "heap" or "stack" the standard does not recognise VLAs as valid, so from that point of view it could be anything since they don't actually exist. In terms of gcc extensions; it's probably on the stack. But, you are outside of C++ - anything goes. – Jesper Juhl Nov 21 '17 at 18:23