tags:

views:

270

answers:

5
  1. Is there C99 faq like c-faq?
  2. Is it a good idea to use C99 VLA? When is it appropriate to use VLA compared to malloc/free? (since VLA may blow up stack?)
A: 

The primary advantage with stack allocation is that you get automatic memory management of the allocated variable-length array. Since memory management is one of the core challenges for any C program, you should definitely use VLA to simplify your task, if you can.

I will then advocate that you should use VLA's consistenly when you can, and otherwise use malloc only if: You need to control the duration of the storage, and if you have very large allocations, and if you want to handle out-of-memory errors gracefully.

kaizer.se
+5  A: 

Yes, except in cases where you know your stack can blow up. You can also change the size of the stack if necessary, it's different how on every OS but it's possible. The advantages of VLA are:

  • Fast : adjusting the stack pointer and/or the frame pointer would have been done anyway so the cost of a VLA is nearly 0.

  • Easy : a simple definition, no pointer to initialize, to check to free and no risk of memory leaks.

  • It's automatically thread safe as each thread has its own stack. It has also better scaling as there's no need of locking, one problem that can arise when using malloc/free.

  • Readable : it's really a simple concept, so less likely to introduce subtle bugs.

It has some drawbacks:

  • Size limited : as already said, the stack can blow up.

  • Buffer overflows are a bit more serious than on heap memory (one can argue that it's an advantage, as a crashing application is better than a one silently corrupting data and eventually crashing on unrelated instructions).

  • Portability : not all compilers implement it, but it can often be simulated by alloca (attention the semantic is a little bit different but nothing really serious).

tristopia
A: 
  1. Googling brought me some FAQ, but I don't know how good they are. Most seem to emphasize just on the difference to previous versions of C. See eg http://www.comeaucomputing.com/techtalk/c99/

  2. As already pointed out by others, this may blow up your stack. And different of a normal array overrun, this might corrupt the stack of any other function on your execution stack. (My experiences are mainly with gcc that has this feature since ages) I had really nasty bugs because of this. You can't control what is happening on another machine, with another compiler... As soon as the size of the array is application dependent (and thus really varying) you will see errors. So my current strategy is to prefer malloc and to use valgrind to check that I didn't forget to free any such arrays on the run.

Jens Gustedt
A: 

For a good list of C99-related links (including links to information about variable-length arrays) see:

Xcode now defaults to use C99 - so what's C99?

http://lists.apple.com/archives/xcode-users/2008/May/msg00665.html

ceeit
A: 

C++ does not support VLAs. So it will be little more effort to port the code to C++, should the need arise.

Then again, some believe this is actually a good thing and cunningly propose "class" as a wonderful name for a symbol in c :-)

dantje