[lkml]   [2018]   [Oct]   [28]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
Messages in this thread
SubjectRe: [Outreachy kernel] [RESEND PATCH 2/2] staging: vboxvideo: Use unsigned int instead bool
On Sun, Oct 28, 2018 at 09:47:15AM +0100, Julia Lawall wrote:
> > The "possible alignement issues" in CHECK report is difficult to figure
> > out by just doing a glance analysis. :)
> >
> > Linus also suggested to use bool as the base type i.e., `bool x:1` but
> > again sizeof(_Bool) is implementation defined ranging from 1-4 bytes.
> If bool x:1 has the size of bool, then wouldn't int x:1 have the size of
> int? But my little experiments suggest that the size is the smallest that
> fits the requested bits and alignment chosen by the compiler, regardless of
> the type.

Yes, correct!
And we can't use sizeof on bitfields *directly*, nor reference it using a

It can be applied only when these bitfields are wrapped in a structure.


#include <stdio.h>
#include <stdbool.h>

struct S {
bool a:1;
bool b:1;
bool c:1;
bool d:1;

int main(void)
printf("%zu\n", sizeof(struct S));

Output: 1

If I change all bool to unsigned int, output is: *4*.

So, conclusion is compiler doesn't squeeze the size less than
native size of the datatype i.e., if we changed all members to
unsigned int:1,
total width = 4 bits
padding = 4 bits

Therefore, total size should have been = 1 byte!
But since sizeof(unsigned int) == 4, it can't be squeezed to
less than it.

> bool x:1 has the advantage that anything that is not 0 is considered true.

Yes, implicit conversion rules for boolean.

> So for bool x:1, x = 4 is true, while for int x:1, x = 4 is false.

Well, int x:1 can either have 0..1 or -1..0 range due implementation
defined behavior as I said in the previous reply.

If you really want to consider negative values, then make it explicit
using `signed int x:1` which make range guaranteed to be -1..0

Regardless, integer conversion rules will kick in.

> But the :1 adds instructions, so at least for only one bool, where little
> space is saved, it is probably not worth it.

True, we should reply on a promised guideline rather than possibility.

Himanshu Jha
Undergraduate Student
Department of Electronics & Communication
Guru Tegh Bahadur Institute of Technology

 \ /
  Last update: 2018-10-28 12:21    [W:0.062 / U:0.992 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site