4 ms·
Everyone makes mistakes, but trying to write correct code is important. You have to be conscious of the consequences of what you write. You shouldn't rely on no
by 0xTJ 6y ago
Everyone makes mistakes, but trying to write correct code is important. You have to be conscious of the consequences of what you write. You shouldn't rely on non-fixed-width types having specific lengths or on signed integer overflow, unless your code guarantees that things will work (preprocessor/static_assert checks against standard/compiler-specific properties). The worst offenders I see often is violating aliasing rules, especially in ways that could cause alignment issues.
In C, function isn't an object, you can't convert a function pointer as a void pointer and back, and use it. Yet I've seen that done many times, and it's probably safe on things that act as if they have a von Neumann architecture.
If I'm relying on implementation-dependent functionality, I try to make it so that whatever it is will fail to compile instead of having incorrect behaviour.
- zabzonk 6y agoBetter than you being aware, make sure your build system is aware - for example, compile your code with the maximum level of warnings and errors turned on, and don't commit it until you have resolved them all.
- zoomablemind 6y ago+1. Yet this should be done from the very start of the project. Too often projects start in a permissive way, and then get released. At that point switching to strict way may become an unsurmountable task practically or politically.
- 0xTJ 6y agoDefinitely, `-Wall -Wextra -pedantic` are always there, and only turning off individual ones if there's a good reason.
- gpderetta 6y ago> In C, function isn't an object, you can't convert a function pointer as a void pointer and back, and use it. It is not defined by the C standard, but POSIX requires it. So any environment (including the compiler) claiming POSIX conformance must support it. Not sure how it works on Itanium though as, IIRC, function pointers there are twice as wide as normal pointers.
- pjmlp 6y agoPlenty of platforms don't care about claiming POSIX conformance.