[PD-dev] Bit fields in t_editor

Christof Ressi info at christofressi.com
Fri Feb 14 10:34:55 CET 2020


To clarify: The compiler is allowed to pack adjacent bitfields (and will 
probably do it), because that's the whole point of using bitfields. The 
whole set of bitfields, however, will be aligned based on the int type. 
You definitely don't have to manually add padding.

Further reading: https://en.cppreference.com/w/c/language/bit_field

Christof

On 14.02.2020 03:54, Martin Peach wrote:
> On Thu, Feb 13, 2020 at 9:24 PM Henri Augusto Bisognini
> <msndohenri at hotmail.com> wrote:
>> Quick C question: i was taking a look at struct _editor and I've noticed that the bitfields sum up to  6 with no (2 bits) padding for alignment.
>>
>> typedef struct _editor
>> {
>>      t_updateheader e_upd;           /* update header structure */
>>      [...]
>>      unsigned int e_onmotion: 3;     /* action to take on motion */
>>      unsigned int e_lastmoved: 1;    /* one if mouse has moved since click */
>>      unsigned int e_textdirty: 1;    /* one if e_textedfor has changed */
>>      unsigned int e_selectedline: 1; /* one if a line is selected */
>>      t_clock *e_clock;               /* clock to filter GUI move messages */
>>      int e_xnew;                     /* xpos for next move event */
>>      [...]
>>
>> } t_editor;
>>
>>
>> All the resources I've found on google used some padding (either manually or with a zero value).
>>
>> My guess here is that since the next member is not a bit field then the compiler will align it for you. Is that right?
> They are all unsigned ints with some of their bits used, so they are
> aligned already to whatever bit-size ints are.
>
> Martin
>
>
>
> _______________________________________________
> Pd-dev mailing list
> Pd-dev at lists.iem.at
> https://lists.puredata.info/listinfo/pd-dev





More information about the Pd-dev mailing list