C bitfields guarantees

Walter Bright newshound2 at digitalmars.com
Fri Jul 5 16:35:43 UTC 2024


On 7/4/2024 10:37 PM, Richard (Rikki) Andrew Cattermole wrote:
> Today many people have spent some time to try and understand Walter's belief 
> that C is "good enough" for bit fields in terms of guarantees.

It's straightforward. If you use uint as the field type, you'll get the same 
layout across every C compiler I've ever heard of.

The reason for this is straightforward:

1. it's the obvious way to do things
2. professional C compiler developers are sensible people
3. professional C compiler developers want to compile existing code and have it 
behave the same way on the same platform, they don't care to antagonize their users

The differences crop up when using multiple field types *and* porting to a 
different ecosystem. These problems are trivially avoided. Even so, within a 
particular ecosystem, the C compilers are all compatible with each other. Why? 
Because C compiler developers want their compiler to be useful!

Is anyone surprised that gcc/clang/ImportC work exactly the same on each ecosystem?

Consider also that the C standard does not specify the size of a 'char'. There 
are C compilers for special CPUs that have different char sizes - notably 32 bit 
chars for some DSP processors, and 10 bit chars for the CPU on a Mattel 
Intellivision game computer. C on a PDP-10 has 36 bit ints, too! and 18 bit shorts.

I can pretty much guarantee that all C code developed on a conventional CPU will 
fail to work on those machines.

But so what. When you port to a diverse machine, you expect such problems.


More information about the Digitalmars-d mailing list