Non-constant array declaration

I have been teaching programming for several years, and I was sure that if you need to declare an array with a variable number, you need to use malloc or new .

Today I found that this compiles under g ++ version 4.4.4 with no warnings or errors:

 #include <iostream> using namespace std; int main() { int size_array; cin >> size_array; int iTable[size_array]; for(int i=0;i < size_array;i++) iTable[i]=i*i; for(int i=0;i < size_array;i++) cout << iTable[i] << endl; return 0; } 

It also compiles completely if you use gcc (after changing cout and cin with printf and scanf )

In Visual Studio, this code does not compile because size_array not a constant.

When was this changed? Is this a safe method?

+7
source share
8 answers

This is a function of C99 - VLA - which is not part of standard C ++. You can use it if your compiler supports it and you do not need portability. If the compiler supports it, it is completely safe to use - but it is a bad habit using non-standard functions.

+9
source

This is a gcc compiler extension , not a standard one.

+5
source

See http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.20 .

Simply put, on C99 this is called VLA and is part of the standard (correct me if I am wrong), but in C ++ this is not part of the standard. If you need this functionality, use std:vector .

+2
source

No, it is absolutely safe. This can damage your stack.

+1
source

It depends on whether you are writing C or C ++. I assume that C for C ++ is better for you to use std :: vector, rather than an array.

In C, it depends on which option you are using. If and only if you use the standard C99 compiler, then the array can take its size from the variable at run time, as you do, otherwise the size must be determined at compile time. Visual Studio does not support dynamic array - see MSDN

C ++ uses the C89 standard, so it requires that the size be set at compile time.

So, in your case, you need to see which flags you passed to the compiler.

As @Eric noted, C ++ code, so the working compiler uses a non-standard extension, so for gnu I would add flags to enforce the standard, like -ansi or -std = C ++ 98 and -pedantic

0
source

There is a GCC extension that mimics the variable lengths of the C99 array. This is not standard C ++.

However, even if you have it turned off, the published code may compile. The standard does not require diagnostics for this case: Undefined Behavior, not an error.

In really obvious cases, the compiler may prevent you from writing this, but otherwise it may allow you to fail.

Conclusion: do not be fooled by compilation. This is still bad and wrong.

0
source

You can get this functionality in C or C ++ using alloca (_alloca on Windows). std :: vector is NOT a replacement: it is allocated on the heap, with a new one that calls malloc, which is potentially expensive.

There is a good reason why you might need an array whose length is determined at runtime allocated on the stack: it is very fast. Suppose you have a loop that runs often, but has an array that depends on something at runtime (say, the size of the canvas widget). You can't just copy the code: your program will crash when we all get 36-inch Retina monitors with Retina display and the [2400] pixels are no longer safe. But you do not want new ones, or your loop is malloc and slows down.

Although for large arrays it might be better to have std :: vector, which is static for the function, and only if necessary, change the size (larger), since your stack has a limited size.

(see http://msdn.microsoft.com/en-us/library/wb1s57t5(VS.71).aspx )

0
source

It is good to use this function if you are using c99.

You must be very careful about size. great value will overflow your stack, and your process may go crazy.

0
source

All Articles