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?
Cheers, Henri.
On Thu, Feb 13, 2020 at 9:24 PM Henri Augusto Bisognini msndohenri@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
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@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@lists.iem.at https://lists.puredata.info/listinfo/pd-dev