2 ms·
hmmmm, no complex numbers, no vla... not really what many people call modern C nowadays.
by enriquto 6y ago
hmmmm, no complex numbers, no vla... not really what many people call modern C nowadays.
- saagarjha 6y agoDo people actually use those?
- kevin_thibedeau 6y agoVLAs are a bad idea and nobody doing modern C should be touching them. It's alloca() magic and introduces all sorts of stack destroying risk.
- ChickeNES 6y agoIsn't there a plan to remove them in the next C standard?
- pjmlp 6y agoAlready done in C11, they were marked optional, so any compiler that jumped from C89 to C11 (we are on the edge of C2x now), isn't required to even bother themselves with VLAs and will still achieve C11 certification (C17, C2X,...)
- enriquto 6y agoPutting VLA in the stack is just an implementation choice. A conforming implementation could allocate all VLA in the heap (and freeing the memory upon scope loss), or do not even use a stack/heap system at all. In that sense, VLA are a crucial language feature to allow a RAII-style for C code. Essentially, you could replace this void f(int n, ...) { float *t = malloc(n * sizeof*t); // do stuff with t[i] free (t); } with the following semantically equivalent code void f(int n, ...) { float t[n]; // do stuff with t[i] } Today, this is not a good idea for large arrays because, unfortunately, many C implementations put VLA in the stack. I hope that by keeping VLA in the standard language, future C implementations will get to favor this usage. Notice that VLA are not more dangerous or risky than recursion. The exact same argument that you use against VLA can be used against recursion. Are you against recursive functions in C? Of course, you wouldn't traverse a long array using recursion, but if you are careful that the depth of recursion is logarithmic with the array size (e.g. as in binary search), then it is perfectly safe and reasonable to use recursion. The same thing with VLA. If you are sure that n is 2,3 or at most 4 there's no reason to avoid VLA due to stack scare, and they may make your code much simpler, clearer and better. On the other hand, VLA are useful even if you do not use them to allocate memory. Pointers to vla can be used for many things, for example accessing a one-dimensional array (allocated by a single call to malloc) using two indices as if it was a 2d array.
- pjmlp 6y agoVLAs are no longer in the standard since C11. Yes they are still there are optional, which basically means that other than clang and gcc, no other C compiler is going to bother supporting them anyway. Google has sponsored a 2 year effort to remove all VLAs from Linux kernel, that is how much love they get from the security industry.
- enriquto 6y ago> other than clang and gcc, no other C compiler is going to bother supporting them anyway. I understand that you have some sort of personal grudge against VLA, but there's no need to use misinformation to further your agenda. VLA are supported by all C compilers except Microsoft's. More precisely, they are supported by gcc, clang, icc, tcc and oracle developer studio (suncc), as well as several other experimental C compilers that seem to be written for fun as a learning experience. I understand that it makes sense to avoid some language features on an OS kernel, like VLA or recursion. For example, it would make sense to avoid types of ambiguous size such as "int". This does not mean in any way that using "int" is wrong in all cases. Many algorithms (I think about mathematical and numerical codes) are very naturally expressed using these fancier language features. You can make a very valid case for language simplification and against VLA and other "modern" features. But your argument is less powerful if you sound ignorant of the widespread support and uses of these features.
- pjmlp 6y agoGiven that they were removed from the standard, yes modern.