4

This is going to be some what of a newbie question but I was trying to work on a small exercise in the C Language (not C++) and I was running into some issues.

Say I wanted to use an array within a method whose size depended on one of the arguments:

void someFunc(int arSize)
{
    char charArray[arSize];
    // DO STUFF
    ...
}

When I try to compile this as a .c file within Visual Studio 2013 I get an error saying that a non-constant array size is not allowed. However the same code works within CodeBlocks under a GNU Compiler. Which should I trust? Is this normal for compilers to behave so differently? I always thought that if you're doing something that a compiler doesn't like you shouldn't be doing it in the first place because it's not a standard.

Any input is useful! I come from a Background in Python and I am trying to get more heavily involved in programming with Data-Structures and Algorithms.

My platform is Windows as you can probably tell. Please let me know if this question needs more information before it can be answered.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740

4 Answers4

9

Variable length arrays(VLA) are a C99 feature and Visual Studio until recently did not support C99 and I am not sure if it currently supports VLA in the lastest version. gcc on the other hand does support C99 although not fully. gcc supports VLA as an extension outside of C99 mode, even in C++.

From the draft C99 standard section 6.7.5.2 Array declarators paragraph 4:

[...] If the size is an integer constant expression and the element type has a known constant size, the array type is not a variable length array type; otherwise, the array type is a variable length array type.

Community
  • 1
  • 1
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
  • 1
    gcc does not fully support C99 either. It just supports (a lot) more features than MSVC. – Renan Gemignani Aug 29 '13 at 21:44
  • @RenanGemignani Correct, still updating. – Shafik Yaghmour Aug 29 '13 at 21:45
  • Thank you for a concise response. I'm also running into issues like the Visual Studio compiler telling me that strcpy is not allowed and I should try strcpy_s for safety. Any chance you could elaborate if that is part of the difference between C99 and not? Also, if I had wanted to do a similar operation in C using an older standard, would I be forced to declare an arbitrarily long array that would fit all my needs? – Валера Горбунов Aug 29 '13 at 22:02
  • 1
    @ВалераГорбунов for `strcpy_s` issue see this [thread](http://stackoverflow.com/questions/4012222/c-strcpy-gives-a-warning-c4996) and your alternative for VLA is to use `malloc` there should be plenty of threads on SO on using `malloc` and arrays. – Shafik Yaghmour Aug 29 '13 at 22:07
3

It depends upon the particular standard your C compiler is following.

The feature you want is called variable length array (VLA) and was introduced into the C99 standard.

Maybe your Visual Studio is supporting some earlier version of the standard. Perhaps you might configure it to support a later version.

Notice that using VLA with a huge size could be a bad habit: VLA are generally stack allocated, and a call frame stack should usually have a small size (a few kilobytes at most on current processors), especially for kernel code or for recursive or multithreaded functions. You may want to heap-allocate (e.g. with calloc) your array if it is has more than a thousand words. Then you'll need to free it later.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
3

You should trust the compilers that you're using and that you want to support.

On that particular issue: non-constant array sizes are valid in C99, which isn't fully supported by either gcc or by MSVC (Microsoft's C/C++ compiler). gcc, however, has this feature from the standard implemented even outside of C99 mode, while MSVC hasn't.

Renan Gemignani
  • 2,683
  • 1
  • 21
  • 23
  • 2
    gcc support for C99 is [quite good](http://gcc.gnu.org/c99status.html), though not quite 100%; most of the "missing" features in that table are not actually required. It's no longer really an extension. – Keith Thompson Aug 29 '13 at 21:45
  • c99 variable length arrays are fully supported with `gcc` see http://gcc.gnu.org/c99status.html – ouah Aug 29 '13 at 21:47
0

This is a GCC extension acting on you.

user2722968
  • 13,636
  • 2
  • 46
  • 67
  • 1
    Not really. As the GCC documentation you link tells, it is a C99 feature (which indeed gets supported by GCC, even in older than C99 mode as an extension). – Basile Starynkevitch Aug 29 '13 at 21:43
  • Not exactly. It was a gcc extension before the 1999 ISO C standard came out; then it became a standard feature of the C language. (Arguably it's still a gcc extension if you compile with `-std=c90`, but that's iffy.) – Keith Thompson Aug 29 '13 at 21:44