Is there a difference between the terms: Dynamic Array and Variable Length Array?

Hello there!

While looking at the google C++ style guide I noticed they mention they don't allow variable length arrays (VLAs) in their code.

I was just wondering if that term: VLA is the same as Dynamic array ? Or is there a clear difference? if so, please tell me about it.

Thanks,

J
There is a difference. A VLA is declared almost like any array, a Dynamic array uses malloc/free to allocate the memory. A VLA is different from a standard array in that it doesn't require a compile time constant for it's size. And remember that VLA are only required to be supported in the C99 compiler, they are optional in C11 and not allowed pre-C99. They have never been part of the C++ standards.

Thanks jlb,

Is it true that VLAs, can only exist inside functions? never globally scoped...

Another thing, I notice that you mentioned the malloc/free combo for memory allocation. I thought that the right way to work with memory allocation in C++ was via: new/delete.. Am I right about preferring the latter?

Thanks again for you reply!
Last edited on
Yes, VLA can only exist in function scope. They also have nothing to do with C++ (which is probably why jlb brought up malloc, comparing features of the same language)

Also, keep in mind that while that Google guide is an interesting read if you're trying to guess how badly broken their code base is, it's terrible as far as C++ style is concerned.
keep in mind that while that Google guide is an interesting read if you're trying to guess how badly broken their code base is, it's terrible as far as C++ style is concerned


Hi Cubbi, I'm sincerely incredibly interested that you expand on that comment of yours. Why is Google's code so terrible? I'm really really curious,

Thanks for your kind reply !
The most glaring problem is that because they have to link with old broken code which is "not prepared to deal with exceptions", they decided to forbid exceptions outright, which means they cannot write meaningful constructors, which means they cannot use C++ resource management, which means they miss out on the main reason C++ exists at all.

There are several other serious problems and simply ridiculous statements ("compiler will do it for you, badly" or "streams are a replacement for printf"), but that's the biggest one. There are a few good points, but the signal-to-noise ratio is pretty bad.

If you're looking for a C++ guide that's useful elsewhere, read a book, such as "C++ coding standards"
Thank you very much, those are interesting thoughts. :) I appreciate you taking the time!

Have a nice day!
Topic archived. No new replies allowed.