Paweł Sołtysiak
Paweł Sołtysiak

Reputation: 625

Non-const declaration of array

I have been teaching myself programming for couple of years, and I was sure that if you need array declaration of a variable number you need to use malloc or new.

Today I found that this compiles under g++ version 4.4.4, without 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;
}

Also it compiles completely fine if you are using gcc (after changing cout and cin with printf and scanf)

Under Visual Studio this code fails to compile since size_array is not constant.

When this was changed? This is a safe method?

Upvotes: 8

Views: 4081

Answers (8)

chenju
chenju

Reputation: 69

It's okay to use the feature if you are using c99.

You have to be very careful about the value of size. a large value will overflow your stack, and your process may go insane.

Upvotes: 0

prewett
prewett

Reputation: 1647

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

There is a good reason why you might want to have an array whose length is determined at runtime allocated on the stack: it's really fast. Suppose you have loop that executes frequently but has an array that depends on something at runtime (say, the size of your canvas widget). You can't just hard-code a number: your program will crash when we all get 36" 300 dpi Retina-display monitors and pixels[2400] is no longer safe. But you don't want new, or your loop hits a malloc and gets slow.

Although, for large arrays, it might be better to have a std::vector that is static to the function an only gets resized (larger) when necessary since your stack has limited size.

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

Upvotes: 0

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385194

There is an extension of GCC mimicking C99 Variable Length Arrays. It's not standard C++.

However, even if you have this turned off, the posted code can compile. The standard does not require a diagnostic for this case: it's Undefined Behaviour, not an error.

In really obvious cases a compiler may choose to prevent you from writing this, but otherwise it is free to let you fail in peace.

Conclusion: don't be fooled by the compilation. It's still bad and wrong.

Upvotes: 0

mmmmmm
mmmmmm

Reputation: 32681

This depends if you are writing C or C++. I'll assume C as for c++ you would be better off using std::vector rather than an array.

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

C++ uses the C89 standard so requires the size to be set at compile time.

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

As noted by @Eric the code is C++ so the working compiler is using a non standard extention so for gnu I would add flags to enforce a standard e.g. -ansi or -std=c++98 and -pedantic

Upvotes: 0

Erik
Erik

Reputation: 91270

This is a C99 feature - VLA - which is not a part of standard c++. You can use it if your compiler supports it and you don't require portability. If the compiler supports it, it's perfectly safe to use - but it's a bad habit using non-standard features.

Upvotes: 12

helpermethod
helpermethod

Reputation: 62185

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

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

Upvotes: 2

Jon
Jon

Reputation: 437386

This is a compiler extension of gcc, not standard.

Upvotes: 5

Daniel A. White
Daniel A. White

Reputation: 190941

No thats not safe at all. It could corrupt your stack.

Upvotes: 2

Related Questions