[MOAB-dev] [Fathom] variable-length arrays

James Porter jvporter at wisc.edu
Tue Nov 17 15:47:52 CST 2009


On Tue, 2009-11-17 at 14:55 -0600, Jason Kraftcheck wrote:
> I believe there are more portability issues with alloca, as it is a glibc
> extension that is not part of any standard.

It seems to be fairly commonly implemented, but you could at least use
alloca() as a fallback for systems that don't support C99 extensions in
C++.

> c) this feature will be included in the next C++ standard

I missed this from your original message the first time around, but
based on my reading of N3000 (released Nov 9, 2009) C++0x does *not*
support VLAs. There are a couple similar things, like constexpr support,
allowing functions to return compile-time constants and std::array
(constant-sized array class), but unless I'm missing something
important, there doesn't seem to be any reference to VLAs at all.

>From a more practical perspective, I think VLAs are probably safe
provided we put them in a macro that falls back to alloca() (or even
malloc()) in places where it's not supported. This would probably (at
least) include GCC if compiled under -std=c++98, which you can detect
via the preprocessor macro __STRICT_ANSI__. That's all assuming that we
can guarantee that we won't overflow the stack, of course.

> The scope of the memory is also much more clear (less error prone) with VLAs
> then with alloca().

True, but the scope of alloca() should always be at least the scope of a
VLA.

> I just tried it in VC++ 2008 and the compiler seems to implement this feature.

Really? Microsoft closed it as won't-fix here:
http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=333273

There does seem to be some very faint rumblings that VS2010 might
support VLAs, but their focus is going to be on C++0x:
http://social.msdn.microsoft.com/Forums/en-US/vcprerelease/thread/6099f453-db2c-49c3-b59a-b799c379cebb

It's possible that it does work (or at least for some cases), but
there's no real guarantee that it behaves at all like the standard
specifies, since Microsoft hasn't explicitly supported it.

- Jim




More information about the moab-dev mailing list