C bitfields guarantees

Daniel N no at public.email
Wed Jul 10 07:55:04 UTC 2024


On Wednesday, 10 July 2024 at 07:43:40 UTC, Daniel N wrote:
> On Wednesday, 10 July 2024 at 07:09:10 UTC, Walter Bright wrote:
>> I had written a detailed reply, but realized you and I were 
>> simply running around in the same circle saying the same 
>> things.
>
> Maybe some input from 3rd party could help?
>
> I use bitfields daily and never had any issues. What I do is to 
> always use fix size types and then simply take all freedom away 
> from the compiler.
>
> uint32_t a;
> uint32_t  :32; // Forced padding
> uint64_t b:10;
> uint64_t c:10;
> uint64_t  :44; // Forced padding
> uint32_t d;
>
> I guess one can use 0 size bitfields also but I usually prefer 
> to visualize how much padding remains for potential future use.

PS To avoid relying on convention, you could make an incomplete 
bitfield a compilation error in D, then D bitfields would have C 
layout *AND* be deterministic.



More information about the Digitalmars-d mailing list