views:

186

answers:

6

I have the following code:

...
int n;
cin >> n;
int numbers[n];
...

It compiled with netbeans on mac using g++ (I think) and it didn't compile using vs2008 on windoze. Why is it so hard to make it work with every compiler? The size of the array is known before allocating it.

EDIT: I know abot std::vector. Actually this was part of a homework and i started it at work on a mac, then got home and was surprised that it didn't work on vs2008. Thanks for all the answers. But I still find it logical that if the compiler can generate some code like alloc(123) where the value 123 is hardcoded, why can't it generate something like alloc(n) where you get n from a memory address that holds an int n or something. It just seems more logical to allow something like this by default.

+12  A: 

Because the size of an array must be a compile time constant in standard C++ (see 8.3.4 §1).

FredOverflow
+14  A: 

Although the size of the array is known before it is allocated, it's still not known until runtime. This is known as variable length array (VLA) and is a C99ism, supported in g++ by an extension that is enabled by default. To be explicit, this is not conformant C++ 98/03, and thus Visual C++ is well within its right to reject it.

If you really want runtime dynamic sizing, allocate on the heap (via new[]). That will work everywhere, and as a bonus, protect you from stack overflows.

Drew Hall
Actually, don't use `new[]`. Use `std::vector`.
GMan
@GMan--I wholeheartedly agree! In my haste I neglected to mention that option, but don't want to cherrypick RC's answer at this point! :)
Drew Hall
I've had to work around this lack myself, though, while implementing data structure insert/delete algorithms. These are heavily used, including in inner loops, so definitely performance hotspots. I needed to build a "plan" (don't start changing nodes until you know the op will succeed - ensure self-consistency on failure). In the end I used a linked-list on the stack, built an item at a time using recursive calls (taking pains to minimise call overheads). A VLA would probably have been easier.
Steve314
@Steve: That's hardcore! I probably would have used an extra std::vector data member in the data structure class to use as a working buffer.
Drew Hall
@Steve314: You can use alloca to roll your own VLAs.
DeadMG
+1  A: 

Support for VLAs is not present in Visual Studio 2008.

Michael Foukarakis
Or in C++ at all.
GMan
+12  A: 

why not use some cpp real stuff like std::vector<int> for that

RC
+2  A: 

Something similar can ne done with

 int* numbers = (int*)alloca(n * sizeof(int));  // equivalent to int numbers[n]

this is not recommended function, but if used carefully, gives exactly the same result.

ruslik
And with carefully crafted input, the user can overflow the stack and score one for the bad guys.
Franci Penov
@Franci, don't you have nothing better to do than to design the "carefully crafted input" for this particular program? :)Better try http://www.crackmes.de
ruslik
There is an important difference between VLA and alloca(). VLA persist in their scope of declaration, while the alloca() block persists for the scope of the *function*. Declaring a VLA in a loop body uses therefore less stack as doing an alloca in the body. So if you learn about the required array size in the loop, alloca might not work for you.
Luther Blissett
@ruslik - part of what I am paid for is actually thinking about the potential effect of "carefully crafted inputs". :-)
Franci Penov
@Franci :))Well, but in this case the security hole would be exactly the same as in "int numbers[n]", so you can't blame me for that ;)
ruslik
ah, but the other one does not compile in my world :-)
Franci Penov
then mine is better, because it at least compiles :)
ruslik
And as an old russian proverb says, an educated man is someone who knows how to play the bagpipe, but doesn't do it.
ruslik
+3  A: 

By book the array dimension should be a constant expression whose value is greater than or equal to one. Constant expression in the sense integral literal constants, enumerators or const objects of integral type that are themselves initialized from const expressions. A non const variable whose value is not known until runtime can not be used to specify the dimension of an array.

But the compiler version i use allows the allows the way you mentioned.

Prabhu Jayaraman