1

So for example,

int aaa(unsigned int par1, unsigned int par2){
  int array[par1*par2];
  return 0;
}

I tried compiling the code that contains this, and it compiled well and had no run-time issue - the array was properly created.

But I know that this is basically declaring a dynamic array in static array declaration fashion. What may go wrong with this declaration? Will there be a compiler issue in a different compiler?

1
  • There will be a compiler issue if the chain you're using doesn't support VLA's. Fwiw, a C11 implementation can legally claim it does not support the feature by defining __STDC_NO_VLA__. The usefulness of that is arguable (at least to me), as a failure to compile would be a certain indicator regardless. Commented Apr 9, 2015 at 1:45

3 Answers 3

5

This is correct as per the 1999 C standard. The 2011 standard changed it to be an optional feature, which in practice means that all C11 compilers will support it except for MSVC.

Before 1999 some compilers offered this as a non-standard extension.

Here is a link to StackOverflow search for other questions on this topic.

Sign up to request clarification or add additional context in comments.

Comments

2

But I know that this is basically declaring a dynamic array in static array declaration fashion.

This is called VLA (Variable-Length Array). This array is not static - it is placed in automatic memory (also known as "the stack").

What may go wrong with this declaration?

It may overflow the stack when the size of the array exceeds a certain limit specific to your system.

Will there be a compiler issue in a different compiler?

This is a C99 feature, so compiler that do not support C99 may fail to compile this code.

Comments

0

I believe this is a C99-only feature. It's called VLA (variable-length arrays).

The C++0x does't support VLA: https://groups.google.com/forum/#!topic/comp.std.c++/K_4lgA1JYeg

But some compilers might not be strictly compliant with the standard.

1 Comment

C++0x is ancient history now; you could talk about C++11 or C++14 which have been released (without VLAs), or C++1z which is scheduled for 2017. However this is a C question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.