1

I am reading a book saying that in C++ you can't do this:

int array_size = 3;
int array[array_size];

Then I tried it with gcc,but it didn't complain at all(exception warned about unused array).

Also I read about this question.The 4th answer says that you can use something like this:char someCondition[ condition ];To me the condition would only be known until runtime,so the whole thing seems really confounding to me.Can anyone help explain this?

Thanks,G

Community
  • 1
  • 1
Gnijuohz
  • 3,294
  • 6
  • 32
  • 47
  • You have enabled the compiler flags to treat as warnings as errors, either turn the flags off or use the `array` in some way to get rid of those warnings/errors. – Alok Save Mar 26 '12 at 04:59

1 Answers1

7

If You are using a C++ compiler it works because most of the C++ compilers provide a compiler extension that supports Variable Length arguments(VLA).

If You are using a C compiler it works because the standard allows it.


In C++, VLA are not allowed by the C++ Standard, so any usage of it through compiler extensions will make your code non portable.
C++ provides std::vector or std::array(C++11) which satisfy all the requirements using variable length array or c-style arrays resp and you should use them.

Note that,since C99 standard, VLA's are allowed in C.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • 1
    +1 for pointing out that he's making use of C99's VLAs, but I don't think it's fair to say that `std::vector` or `std::array` are perfect replacements. `std::vector` involves a heap allocation (potentially quite a heavy cost) whereas `std::array` requires a size known at compile-time. I would love to see VLAs in future versions of C++ as they can be useful where low-level coding is unavoidable, and VLAs do help to avoid exhausting the stack in such cases because a fixed stack array would either use too much stack and overflow the stack or too little and overrun the array. – stinky472 Mar 26 '12 at 04:48
  • I know I got my answer already,but I am wondering if you allow VLA in c,where it would be?Heap or stack?Seems hard to implement this and if you really need VLA,you can do it in other ways.Why c99 would standardize this? – Gnijuohz Mar 26 '12 at 04:54
  • @stinky472: A [proposal](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2648.html) to bring VLA's to the standard was reviewed by the standards committee during C++0x days but was not taken up.Maybe it will be reviewed again for the next standard version,but that seems to be too far away C++12... maybe :) – Alok Save Mar 26 '12 at 04:55
  • @stinky472 you mean VLA is more efficient.But why is that? – Gnijuohz Mar 26 '12 at 04:57
  • @Gnijuohz On the stack, and this is why VLAs can be very efficient. Of course, I would still recommend std::vector as your variable-sized default and std::array as your fixed-size default when it comes to contiguous containers: safety and correctness should always take precedence to speed. However, I work in raytracing fields with a small codebase where correctness isn't too elusive but speed can be, so I dream of VLAs in C++ for that small percentage of code where it can be used (very carefully) as an optimization tool to address heap-related profiler hotspots. – stinky472 Mar 26 '12 at 05:07