9 ms·
That's great. Please don't forget to finish the C99 implementation! For tiny values I really like being able to use int arr[runtime_size]; rather than risking
by pornel 11y ago
That's great. Please don't forget to finish the C99 implementation!
For tiny values I really like being able to use int arr[runtime_size]; rather than risking buffer overflows with int arr[MAX_SIZE] or arr = malloc(size * sizeof(oops)); — and MSVC is the last compiler that still doesn't support that.
- userbinator 11y agoVLAs do not absolve you of the need to ensure the buffer is the right size. In fact, they can be worse since at least malloc()'s failure can be determined from its return value. A VLA will just overflow the stack silently: https://www.clarkcox.com/blog/2009/04/07/c99s-vlas-are-evil/ https://www.clarkcox.com/blog/2009/04/07/c99s-vlas-are-evil/ Edit: to expand on this point, if you really need to keep all that data in memory at once, it's often better to just malloc() a buffer of the appropriate size and reuse it, reallocating if it should be bigger. (Obviously, do not forget to free() it eventually.)
- bluecalm 11y agoThe point is that you sometimes want them on the stack and malloc won't do it for you. MSVC has this already though: https://msdn.microsoft.com/en-us/library/wb1s57t5.aspx https://msdn.microsoft.com/en-us/library/wb1s57t5.aspx so I really don't think there is any good will there. It seems someone in the management is set on "C++ is the future, C is obsolete" view, at least that's the impression I've got from the press releases about it.
- Peaker 11y agoIf you want them on the stack, it's safer to use MAX_SIZE so that you don't blow the stack when some arbitrary input arrives in the future.
- bluecalm 11y ago1)There is sometimes a very slight performance hit as there is more space between arrays and you miss on cache 2)using something which is smaller than MAX_SIZE is never worse unless you write some safety critical software where a bug when you write to unintended but empty location is better than writing randomly somewhere and/or crashing. I prefer the latter in what I am doing (as it's easier to find something is very wrong)
- Peaker 11y agoThe memory hit is an interesting point, could be useful if there's a static validation on worst case stack size. The smaller allocation is worse as it hides potential bugs.
- pjmlp 11y agoYeah, they are so safe that were made optional in C11.
- bluecalm 11y agoThe made it optional because the committee has to cater to the interest group. They are as safe as the rest of C/C++ language: 100% rock solid safe if you know what you are doing and catastrophically unsafe if you don't. The point is that a lot of code uses them and that makes porting it a pain. I mean, they spend so much resources to implement all the new toys in C++ which just introduce the new way of doing the same thing but to actually implement something which is used in real world code, which they already almost have (_alloca) is sometimes a problem because it's "unsafe". No one sane can actually believe that explantion.
- Peaker 11y agoVLAs are just a bad idea. A. They make stack allocation much more dynamic, harder to reason about its bounds, and arbitrary inputs may blow the stack later. B. They make sizeof a dynamic thing! sizeof can no longer always be constant folded and can even cause side effects!
- bluecalm 11y agoThen don't use them when you have arbitrary unbounded inputs or maybe even don't use them if you care about security. In many applications you don't care about those things (like say writing a game engine). They come in handy there. I mean, it's not a some kind of critical feature but some code use it and it's nice to be able to compile it. They are also quite convenient and in rare cases the best solution performance wise.
- uxcn 11y agoVLAs in reality are just as dangerous as allocating from the heap. I would agree not everything should be allocated on the stack, but you can overflow the heap just as you can overflow the stack. Unless people are completely irresponsible, they query the stack limit and subtract the current stack bound before using the heap for large objects. Freeing memory is even simpler than using the heap, and faster as well. I think the only real concern is people being responsible about using them.
- mkup 11y agoYou can use _alloca(), however be careful not to overflow thread stack. This applies to C99 variable length arrays as well.
- uxcn 11y agoActually, alloca is even less portable than VLAs.