[TUHS] FD 2

Steve Nickolas usotsuki at buric.co
Tue Jan 31 02:21:45 AEST 2023


On Mon, 30 Jan 2023, Dan Cross wrote:

> I think C is a language that people _think_ is simple to understand,
> and perhaps _was_ simple to understand, but is actually remarkably
> subtle and a _lot_ of people don't actually have a great handle on how
> it really works anymore. Particularly now, when the compiler people
> seem to be prizing optimization above all else and so even obvious
> behavior that's technically "undefined" results in unexpected behavior
> (e.g., `if (a > 0 && b > 0 && a*b) < 0) overflow(); // signed integer
> overflow is UB`. Maybe sadly, C hasn't been a portable macro assembler
> for decades now.

I always assume compiler braindeath.  Always use parentheses; never assume 
any specific order of precedence.

I've actually been bitten by compilers doing unexpectedly stupid things, 
so I guard around them.  I'll never merge two chars into a short with

   c=(a<<8)|b;

I will ALWAYS do this:

   c=a;
   c<<=8;
   c|=b;

because 16-bit MS-DOS compilers will ALWAYS manage to blunder that, though 
32-bit compilers get it right (the sane thing to do would be to treat it 
as a word bitshift, but the compiler treats it as a byteshift because a is 
uint8_t).

I'll never do if (a==b&&c==d), always if ((a==b)&&(c==d)).

I always try to assume the compiler will wreck my code in hilariously 
braindamaged ways and code so precisely that it cannot do so.

<snip>

> Imitation is the most sincere form of flattery.

The actual saying continues "...that mediocrity can make to greatness", 
which changes the meaning a bit. ;p

-uso.


More information about the TUHS mailing list