2 ms·
For Example #1, C (but not C++) has a syntax for hinting the expected array size (note the warnings): https://www.godbolt.org/z/7bcd9xPr3 https://www.godbolt.o
by flohofwoe 5y ago
For Example #1, C (but not C++) has a syntax for hinting the expected array size (note the warnings):
https://www.godbolt.org/z/7bcd9xPr3 https://www.godbolt.org/z/7bcd9xPr3
For example #2, proper compilers have warnings for implicit conversions which would lose data, for instance:
https://www.godbolt.org/z/vEP8ra9MP https://www.godbolt.org/z/vEP8ra9MP
But for a proper solution to the array size problem you need array slice support built into the language, and this would be the first "opaque builtin struct type" in C (because slice types need either a pointer/pointer or pointer/size pair). At this point it's really better to switch to a different language.
- tialaramex 5y agoBut the compiler cheerfully still calls the function with a pointer instead, even if that pointer is in fact pointing at the too-short array. And yes, it really is better to switch, I've been doing so, and I am hopeful that the kernel can follow in due time. Aside: Matt Godbolt deserves some sort of award for Compiler Explorer. So much less awful to just link these examples in such conversations. It might well be as big a contribution to software engineering as git bisect or Tinderbox, or (really going back) Grace Hopper's "compiler". Yes Compiler Explorer seems "obvious" and people talked about things like this but did you build it? 'cos just talking about it doesn't make my life any better but building it does.
- pjmlp 5y agoIndeed, for Matt Godbolt, and the whole community that has added the remaining AOT languages, .NET is one of the few missing from Compiler Explorer, but it has a similar one at https://sharplab.io/ https://sharplab.io/ and there is one at http://shader-playground.timjones.io/ http://shader-playground.timjones.io/ for GPGPU shaders. Here is one talk from Matt Godbolt about how Compiler Explorer was born, CppCon 2019: Matt Godbolt “Compiler Explorer: Behind The Scenes” https://www.youtube.com/watch?v=kIoZDUd5DKw https://www.youtube.com/watch?v=kIoZDUd5DKw