3 ms·
The C equivalent for Fortran stack-allocated arrays is Variable Length Arrays (VLA) which are (afaik) a GNU extension which is usually considered bad. They are
by xmcqdpt2 3y ago
The C equivalent for Fortran stack-allocated arrays is Variable Length Arrays (VLA) which are (afaik) a GNU extension which is usually considered bad.
They are awesome in Fortran because it means that I can (for example) write a function that takes arrays of size N and stack allocate temporaries of size N on entry. I can pass those to other functions etc and they will be freed automatically when I return to my caller. It's actually a really nice feature.
- camel-cdr 3y agoThe reason VLAs are considered bad is because they stack allocate non fixed size buffers and the stack is limited. The argument goes that one might as well use a fixed size if you need to use enforce upper bound anyways.
- unnah 3y agoIt is possible to heap allocate a VLA too, although the syntax is a bit more cumbersome with pointer access. But it does allow you to pass dynamically sized multi-dimensional arrays to functions and access them with array indexing notation.
- Findecanor 3y agoVLAs on the stack were introduced in C99. They became optional in C11. They are a GNU extension for C++, though. BTW, MSVC never supported C99 or VLAs. Recent versions support C11.
- xmcqdpt2 3y agoThanks, didn't know they were in the standard (for a while anyway).