Possible Duplicate:
C/C++: Array size at run time w/o dynamic allocation is allowed?
I'm in a class where we have a simple assignment to write a function that returns a pointer to a dynamic array and provide a stub to test it. I have this done so I'm not asking for help with the assignment. On the class blog another student suggests that it is intuitive that one should be able to do int Array[size];
where size
is apparently a user defined variable.
I know that in standard C++, at least according to my text, an array must have a "constant integer expression...greater than zero" as a size declarator. Which implies to me that their example can't work. In fact, with VS2010 you get three errors when you try.
However, they explain:
I know that, you know that, even the g++ compiler in my Ubuntu install AND the Bloodshed compiler on my WinXP install know that. For certain levels of "know that" one would expect "int Array[size];" to work (like it has in both the prior classes).
But evidently it fails to run on some people's VS compilers. One can only assume that, since the common denominator is VS (and yes, I confirmed this by asking a friend to check it on HIS box), that VS is the problem. One of several.
There's already confusion because the I know that...
part is referring to me telling them that there shouldn't be any reason to #include <new>
in order to use new
, but it seems they think we agree that there shouldn't be any reason to use new
to allocate a dynamic array.(?)
So the question is obvious. Are there any compilers which will accept int Array[size];
, where size is not a const int
without error?
where
size
is not a constant expression is a variable length array. They're a standard feature of C as of the 1999 ISO standard. The C++ standard has not adopted them, but some compilers support them as a language extension.(Any C++ compiler, when invoked in conforming mode, must at least issue a diagnostic for an array type with a non-constant size.)
Incidentally, this isn't directly related to the IDE you're using; it's controlled by the compiler that your IDE invokes.
As for
#include <new>
there is a standard header by that name, but it's not necessary for thenew
operator, which is built into the language. The<new>
header provides several overloaded versions ofoperator new
and a few other declarations.