A question about a structure containing ':' 
Author Message
 A question about a structure containing ':'

59 sensor[hunsoo]:/user4/hunsoo> cat a.c
#include <stdio.h>
typedef unsigned char uchar_t;

struct byte {
        uchar_t b_port:4,
                b_value:4;

Quote:
};

int
main(void)
{
        struct byte by;

        by.b_port = 5;
        by.b_value = 5;

        printf("0x%2x\n", *(uchar_t *) &by);
        return 0;

Quote:
}

60 sensor[hunsoo]:/user4/hunsoo>
62 sensor[hunsoo]:/user4/hunsoo> make a
gcc -Wall -pedantic -ansi -g    a.c   -o a
a.c:5: warning: bit-field `b_port' type invalid in ANSI C
a.c:6: warning: bit-field `b_value' type invalid in ANSI C
a.c: In function `main':
a.c:17: warning: implicit declaration of function `printf'
63 sensor[hunsoo]:/user4/hunsoo> a
0x55
64 sensor[hunsoo]:/user4/hunsoo>

I am confused by the warning messages of gcc when I compiled
the above a.c because I think I did not make anything wrong.

Anyway what does the line 'a.c:5: warning: bit-field `b_port' type invalid
in ANSI C' mean?  Did I viloate some unknown
rule of the standard c?

------------
Hong Hunsoo
dep.of.physics of KAIST
Korea Advanced Institute of Science and Technology                        



Mon, 21 Aug 2000 03:00:00 GMT  
 A question about a structure containing ':'


Quote:
> struct byte {
>         uchar_t b_port:4,
>                 b_value:4;
> };

[...]

Quote:
> a.c:5: warning: bit-field `b_port' type invalid in ANSI C
> a.c:6: warning: bit-field `b_value' type invalid in ANSI C

[...]

Quote:
> I am confused by the warning messages of gcc when I compiled
> the above a.c because I think I did not make anything wrong.

What's confusing about the messages? They state that the types of 'b_port'
and 'b_value' are invalid. This is correct. Bitfields may be only signed or
unsigned int (technically, the valid types are 'unsigned int', 'signed int',
and 'int', but 'int' is always equivilant to one of the first two).

Quote:
> Anyway what does the line 'a.c:5: warning: bit-field `b_port' type invalid
> in ANSI C' mean?  Did I viloate some unknown rule of the standard c?

This rule is far from unknown, and should definetly have been mentioned in
whatever language reference you are using. If it isn't, I recommend that
you obtain a new one.

--
(initiator of the campaign for grumpiness where grumpiness is due in c.l.c)

Attempting to write in a hybrid which can be compiled by either a C compiler
or a C++ compiler produces a compromise language which combines the drawbacks
of both with the advantages of neither.



Mon, 21 Aug 2000 03:00:00 GMT  
 A question about a structure containing ':'



??o????...

Quote:

> > struct byte {
> >         uchar_t b_port:4,
> >                 b_value:4;
> > };

> [...]

> > a.c:5: warning: bit-field `b_port' type invalid in ANSI C
> > a.c:6: warning: bit-field `b_value' type invalid in ANSI C

> [...]

> > I am confused by the warning messages of gcc when I compiled
> > the above a.c because I think I did not make anything wrong.

> What's confusing about the messages? They state that the types of
'b_port'
> and 'b_value' are invalid. This is correct. Bitfields may be only signed
or
> unsigned int (technically, the valid types are 'unsigned int', 'signed
int',
> and 'int', but 'int' is always equivilant to one of the first two).

> > Anyway what does the line 'a.c:5: warning: bit-field `b_port' type
invalid
> > in ANSI C' mean?  Did I viloate some unknown rule of the standard c?

> This rule is far from unknown, and should definetly have been mentioned
in
> whatever language reference you are using. If it isn't, I recommend that
> you obtain a new one.

> --

Then are the following lines of the /usr/include/netinet/ip.h invalid
in terms of ANSI C?

struct ip {
#if defined(vax) || defined(i386)
        u_char  ip_hl:4,                /* header length */
                ip_v:4;                 /* version */
#endif
#if defined(mc68000) || defined(sparc)
        u_char  ip_v:4,                 /* version */
                ip_hl:4;                /* header length */
#endif
        u_char  ip_tos;                 /* type of service */
        short   ip_len;                 /* total length */
        u_short ip_id;                  /* identification */

-----------------------
Hong Hunsoo
dep.of.physics of KAIST
Korea Advanced Institute of Science and Technology                



Mon, 21 Aug 2000 03:00:00 GMT  
 A question about a structure containing ':'

: 59 sensor[hunsoo]:/user4/hunsoo> cat a.c
: #include <stdio.h>
: typedef unsigned char uchar_t;

: struct byte {
:         uchar_t b_port:4,
:                 b_value:4;
: };

: int
: main(void)
: {
:         struct byte by;

:         by.b_port = 5;
:         by.b_value = 5;

:         printf("0x%2x\n", *(uchar_t *) &by);
:         return 0;
: }
: 60 sensor[hunsoo]:/user4/hunsoo>
: 62 sensor[hunsoo]:/user4/hunsoo> make a
: gcc -Wall -pedantic -ansi -g    a.c   -o a
: a.c:5: warning: bit-field `b_port' type invalid in ANSI C
: a.c:6: warning: bit-field `b_value' type invalid in ANSI C

This means the type's invalid in ANSI C - bitfields must be
some sort of int.  Note that bitfields implementation varies
between compilers, and they are fairly non-portable objects.

: a.c: In function `main':
: a.c:17: warning: implicit declaration of function `printf'

But I don't know about this one.  It sounds as if it isn't
finding printf() in stdio.h, but that seems unlikely.

Will



Mon, 21 Aug 2000 03:00:00 GMT  
 A question about a structure containing ':'


Quote:

> Then are the following lines of the /usr/include/netinet/ip.h invalid
> in terms of ANSI C?

> struct ip {
> #if defined(vax) || defined(i386)
>         u_char  ip_hl:4,                /* header length */
>                 ip_v:4;                 /* version */
> #endif
> #if defined(mc68000) || defined(sparc)
>         u_char  ip_v:4,                 /* version */
>                 ip_hl:4;                /* header length */
> #endif
>         u_char  ip_tos;                 /* type of service */
>         short   ip_len;                 /* total length */
>         u_short ip_id;                  /* identification */

They are not valid ANSI C (assuming that u_char is not defined as unsigned
int, and at least one of vax, i386, mc68000 or sparc is #define'd).

Before you complain to whoever supplied this header file: If you bought a
compiler, and the compiler came together with this file ip.h, and the
compiler documentation says that <ip.h> is a standard header file of this
compiler, then it is a part of the compiler and doesnt have to be legal C
at all - as long as the original compiler accepts #include <ip.h>.



Mon, 21 Aug 2000 03:00:00 GMT  
 A question about a structure containing ':'

Huh.. Why on earth did the ANSI-C makers dicide to consider the
'uchar a:4' as an invalid one.  It seams to me that they did
wrong things, made wrong ANSI-C, wrong, unnatural standard.  I
think if the 'bitfields of an int' are valid, then the 'bitfields
of char' should be valid ones.   Like if there exists an 'unsigned
int', the existence of 'an unsigned char' is natural one.  For me
the bitfields of char is very important because I have to control
each bit pattern of a register in a board.   For example, for
the following a register, I am in a situation of controlling
each bit at my will,

+------+------+------+------+------+------+------+------+
|  W2  |  W1  |  W0  | INTR | DMA  | PORT2|PORT1 |PORT0 |
|      |      |      |      |      |      |      |      |
+------+------+------+------+------+------+------+------+

And I imagined that if I create a structure like follwoing,

struct some_register {
        uchar_t r_wait:3,
                r_intr:1,
                r_dma:1,
                r_port:3;

Quote:
};

them I can manipulate the reigster easily. Therfore your statement
that the bitfield of a char are invalid ones makes me disappointed,
and makes me complain to people who made ANSI-C.   I dare to think
that 'the bitfields of a char' are much much more useful ones than
'those of an int'.  You can see this usefulness of 'bitfields of
char' in the ip structure of /usr/include/netinet/ip.h.  You can
find a lot of examples at that directory if you are unsing a
unix machine.  Is there anybody who has ever made the 'bitfields'
of an int? I have never met a situation in which the bitfields of
int might be useful ones because I can easily split up an integer
into 'char, or short', and I have never met a source code that was
coded using the 'bitfields of int'.

Dont take my words seriously.  I just want to know why did the ANSI-C
dicide to take the 'bitfields of a char' as invalid....

---
Hong Hunsoo
dep.of.physics KAIST
Korea Advanced Institute of Science and Technology



Mon, 21 Aug 2000 03:00:00 GMT  
 A question about a structure containing ':'



...

Quote:
>Then are the following lines of the /usr/include/netinet/ip.h invalid
>in terms of ANSI C?

>struct ip {
>#if defined(vax) || defined(i386)
>        u_char  ip_hl:4,                /* header length */
>                ip_v:4;                 /* version */
>#endif
>#if defined(mc68000) || defined(sparc)
>        u_char  ip_v:4,                 /* version */
>                ip_hl:4;                /* header length */
>#endif

These are not portable. ANSI C allows an implementation to define extra
types that can be used to specify bitfields but it only guarantees
int, signed int and unsigned int. User code should stick to those.
Implementation supplised headers only need to work on the implementation
they are supplied with and therefore don't have to be written in portable
C (possibly not in C at all). System headers shouldn't be used as examples
of how to write code, that should follow the actual language and
interface specifications.

--
-----------------------------------------


-----------------------------------------



Mon, 21 Aug 2000 03:00:00 GMT  
 A question about a structure containing ':'


Quote:
> int', the existence of 'an unsigned char' is natural one.  For me
> the bitfields of char is very important because I have to control
> each bit pattern of a register in a board.   For example, for
> the following a register, I am in a situation of controlling
> each bit at my will,

So just use the unsigned int bit fields, and ignore the top
24 bits (or bottom 24, depending on endianess)!

Quote:
> unix machine.  Is there anybody who has ever made the 'bitfields'
> of an int? I have never met a situation in which the bitfields of

Yes, but not necessarily when addressing an 8 bit hardware register
(although I used unsigned ints).

--

If it ain't analogue, it ain't music.
#include <disclaimer.h>                          \\|// - ?
                                                 (o o)
          /==================================oOOo=(_)=oOOo========\

          |                                                       |
          |                                                       |
          |                          WWW: www.rkdltd.demon.co.uk  |
          |                                 .oooO                 |
          |                                  (  )  Oooo.          |
          \===================================\ (==(   )==========/
                                               \_)  ) /
                                                   (_/



Mon, 21 Aug 2000 03:00:00 GMT  
 A question about a structure containing ':'

Quote:

> +------+------+------+------+------+------+------+------+
> |  W2  |  W1  |  W0  | INTR | DMA  | PORT2|PORT1 |PORT0 |
> |      |      |      |      |      |      |      |      |
> +------+------+------+------+------+------+------+------+

> And I imagined that if I create a structure like follwoing,

> struct some_register {
>         uchar_t r_wait:3,
>                 r_intr:1,
>                 r_dma:1,
>                 r_port:3;
> };

> them I can manipulate the reigster easily. Therfore your statement
> that the bitfield of a char are invalid ones makes me disappointed,
> and makes me complain to people who made ANSI-C.

struct some_register {
        unsigned int r_wait:3,
                     r_intr:1,
                     r_dma:1,
                     r_port:3;

Quote:
};

This does exactly the same thing as your declaration would do if it were
legal. It says that there's a field named r_wait with 3 bits, followed
by some unspecified padding, followed by a field named r_intr with 1
bit, followed by some unspecified padding, followed by a field named
r_dma with 1 bit, followed by some unspecified padding, followed by a
field named r_port with 3 bits.
        There are two important points here. First, it makes no difference at
all whether you're talking about a 3 bit field whose type is unsigned
char or a 3 bit field whose type is unsigned int. They're both three
bits, they're both unsigned, and any arithmetic operations that you
perform on their contents will be done at the precision of an int.
Second, there's no requirement that these fields actually be adjacent to
each other. You must check the code that your compiler generates to see
whether this will actually work. It's not really what bitfields were
intended for, although it often works.
        -- Pete


Mon, 21 Aug 2000 03:00:00 GMT  
 A question about a structure containing ':'

Quote:

> Huh.. Why on earth did the ANSI-C makers dicide to consider the
> 'uchar a:4' as an invalid one.  It seams to me that they did
> wrong things, made wrong ANSI-C, wrong, unnatural standard.  I
> think if the 'bitfields of an int' are valid, then the 'bitfields
> of char' should be valid ones.   Like if there exists an 'unsigned
> int', the existence of 'an unsigned char' is natural one.  For me
> the bitfields of char is very important because I have to control
> each bit pattern of a register in a board.   For example, for
> the following a register, I am in a situation of controlling
> each bit at my will,

> +------+------+------+------+------+------+------+------+
> |  W2  |  W1  |  W0  | INTR | DMA  | PORT2|PORT1 |PORT0 |
> |      |      |      |      |      |      |      |      |
> +------+------+------+------+------+------+------+------+

> And I imagined that if I create a structure like follwoing,

> struct some_register {
>         uchar_t r_wait:3,
>                 r_intr:1,
>                 r_dma:1,
>                 r_port:3;
> };

You could do exactly the same with:
 struct some_register {
         unsigned int r_wait:3,
                 r_intr:1,
                 r_dma:1,
                 r_port:3;
 };

As the actual storage size is given by the bit field the result is the
same.

Quote:

> them I can manipulate the reigster easily. Therfore your statement
> that the bitfield of a char are invalid ones makes me disappointed,
> and makes me complain to people who made ANSI-C.   I dare to think
> that 'the bitfields of a char' are much much more useful ones than
> 'those of an int'.  You can see this usefulness of 'bitfields of
> char' in the ip structure of /usr/include/netinet/ip.h.  You can
> find a lot of examples at that directory if you are unsing a
> unix machine.  Is there anybody who has ever made the 'bitfields'
> of an int? I have never met a situation in which the bitfields of
> int might be useful ones because I can easily split up an integer
> into 'char, or short', and I have never met a source code that was
> coded using the 'bitfields of int'.

I don't think that having bit fields of a char would give you very
much functionally (except a check the size of the bit field is no more
than a char).
This is because when used the integral promotions would mean the char
is promoted to an int giving the same result as you would get if you
used a bit field of int.

On the other hand restricting bit fields so they can't use char seems
a fairly arbitrary restriction. And it could cause inconvenience (though
no loss of function) when using typedefs that you might have to type
the bit field separately.

Now bit fields of long might be useful, then one could reliably
have more than 16 bits in a bit field.

Another way of viewing the type of bit fields would be that the bit field
specification acts like short and long to modify the int type. So you
would only be able to have a bit field of [unsigned | signed] int. Anything
else would be like writing short char or long short int. However that would
rule out using any typedefs for the type of a bit field. I do not think
this is they way the standard currently views it.

--

Philips Semiconductors Ltd                  
Southampton SO15 0DJ                        +44 (01703) 316431
United Kingdom                              My views are my own.
Do you use ISO8859-1? Yes if you see ? as copyright, as division and ? as 1/2.



Mon, 21 Aug 2000 03:00:00 GMT  
 A question about a structure containing ':'



Quote:
>Huh.. Why on earth did the ANSI-C makers dicide to consider the
>'uchar a:4' as an invalid one.  It seams to me that they did
>wrong things, made wrong ANSI-C, wrong, unnatural standard.

[...]

Quote:
>+------+------+------+------+------+------+------+------+
>|  W2  |  W1  |  W0  | INTR | DMA  | PORT2|PORT1 |PORT0 |
>|      |      |      |      |      |      |      |      |
>+------+------+------+------+------+------+------+------+

>And I imagined that if I create a structure like follwoing,

>struct some_register {
>        uchar_t r_wait:3,
>                r_intr:1,
>                r_dma:1,
>                r_port:3;
>};

>them I can manipulate the reigster easily.

I don't understand why you are so angry. What's the problem declaring
your structure as

        struct some_register {
          unsigned r_wait:3,
                   r_intr:1,
                   r_dma:1,
                   r_port:3;
        };

Can't you manipulate now your register as easily ?

Further I think there is still a general misunderstanding. You
probably may think that, because you defined your bit field struct
members of type unsigned int, sizeof(struct some_register) will be
necessarily be equal to sizeof(unsigned int). This is not the case.
The compiler may choose any appropriate addressable storage unit to
"host" an  adjacent bit-field sequence - even a byte/char.

So, in fact, your bit-fields are "mental" objects and the mapping of a
bit-field sequence to a "physical" bit string is not pecified by the
standard. Even the order in which adjacent bit-fields are stored in
their host storage unit is to be defined by the implementation.

Regards
Horst

   *** Las orillas del Nahuel Huapi ***



Mon, 21 Aug 2000 03:00:00 GMT  
 A question about a structure containing ':'


Quote:

>Huh.. Why on earth did the ANSI-C makers dicide to consider the
>'uchar a:4' as an invalid one.  

"unsigned int" specifies type of the field and ":4" specifies the
size.  A character type isn't needed to specify size.

I am using an implementation that allows bitfields of
type (unsigned) char.  The difference between a char bitfield and a
int bitfield is the size of the resulting struct.  The char bitfields
result in the allocation unit being 8 bits, while the int bitfields
result in the allocation unit being 16 bits (int size).  This DOES
make a difference when it is embedded in another structure or array.

The ANSI C89 version of the standard says that "An implementation may
allocate any addressable storage unit large enough to hold a
bit-field."  The implementation thus has license to only allocate a
byte if the declared bitfields fit, but I have not seen such an
implementation.  I think there would be definite benefit for this.


 > struct some_register {
 >         unsigned int r_wait:3,
 >                      r_intr:1,
 >                      r_dma:1,
 >                      r_port:3;
 > };

 > This does exactly the same thing as your declaration would do if it were
 > legal. It says that there's a field named r_wait with 3 bits, followed
 > by some unspecified padding, followed by a field named r_intr with 1
 > bit, followed by some unspecified padding, followed by a field named
 > r_dma with 1 bit, followed by some unspecified padding, followed by a
 > field named r_port with 3 bits.

The standard does not allow for arbitrary padding between bitfields.
They must be ajacent, except that when crossing a boundary the
implementation is allowed to pad out the remaining allocation unit. In
particular, no implementation is permitted to place padding between
r_wait and r_intr, although if the storage unit is 3 bits wide, it may
jump to the beginning of the next storage unit.

Such padding is not required and I know of one implementation in which
fields cross allocation unit boundaries with no padding.  


 > You could do exactly the same with:
 >  struct some_register {
 >          unsigned int r_wait:3,
 >                  r_intr:1,
 >                  r_dma:1,
 >                  r_port:3;
 >  };

 > As the actual storage size is given by the bit field the result is the
 > same.

The difference, for an implementation I am familar with, is that the
size of the struct is sizeof(int) in one case and 1 in the other.

Thad



Mon, 21 Aug 2000 03:00:00 GMT  
 A question about a structure containing ':'

Quote:

> Huh.. Why on earth did the ANSI-C makers dicide to consider the
> 'uchar a:4' as an invalid one.  It seams to me that they did
> wrong things, made wrong ANSI-C, wrong, unnatural standard.  I
> think if the 'bitfields of an int' are valid, then the 'bitfields
> of char' should be valid ones.   Like if there exists an 'unsigned
> int', the existence of 'an unsigned char' is natural one.

As stated by several people on this group, the ISO standard
guarantees that 'int' and 'unsigned int' are the only valid (and
portable) types for bitfields.

However, consider the ramifications of allowing 'char' and 'short'
bitfield types as well.  What happens when you do this (on an 8-bit
system)?:

    struct Bittle
    {
        unsigned char   a: 3;
        unsigned char   b: 9;   /* Oops */
        unsigned short  c: 20;  /* Oops */
    };

It would appear that allowing 'long' and 'long long' bitfield types
would be useful.  But while section 6.5.2.1 of the standard (both
C89 and draft C9X) states that 'int', 'signed int', or
'unsigned int' are the only explicitly allowed types, it does not
state that the width of bitfields is limited to the width of 'int';
rather, it states that "a bitfield is interpreted as an integral type
consisting of the specified number of bits".  This implies that the
bitfield width could be the number of bits in *any* integral type,
including 'long' and 'long long'.

So it looks like C already has long bitfields.  Presumably, then,
the following is portable and conforming:

    struct Widdle
    {
        unsigned int    a: 24;  /* More than the minimum 16 bits */
        unsigned int    b: 8;
    };

    struct Widdle       w;
    long int            i;      /* Will be at least 32 bits wide */

    w.a = 0x00FFFFFF;           /* Set all bits of w.a to 1 */
    w.b = 0x00;                 /* Set all bits of w.b to 0 */

    i = w.a;                    /* Should be no loss of bits */
    assert(i == w.a);           /* Should be true, portably */
    assert(i == 0x00FFFFFF);    /* Should be true, portably */




Mon, 21 Aug 2000 03:00:00 GMT  
 A question about a structure containing ':'


Quote:

>Huh.. Why on earth did the ANSI-C makers dicide to consider the
>'uchar a:4' as an invalid one.  It seams to me that they did

There is insufficient utility in standardizing bit fields other than int or
unsigned int.  

What is the use of a bit-field that is an unsigned char? All that means is
that it can't be wider than CHAR_BIT.

Quote:
>think if the 'bitfields of an int' are valid, then the 'bitfields
>of char' should be valid ones.   Like if there exists an 'unsigned
>int', the existence of 'an unsigned char' is natural one.  For me
>the bitfields of char is very important because I have to control
>each bit pattern of a register in a board.   For example, for
>the following a register, I am in a situation of controlling
>each bit at my will,

>+------+------+------+------+------+------+------+------+
>|  W2  |  W1  |  W0  | INTR | DMA  | PORT2|PORT1 |PORT0 |
>|      |      |      |      |      |      |      |      |
>+------+------+------+------+------+------+------+------+

Since you are accessing hardware, you are no longer writing in portable
C. Using bit-fields to correspond to an external storage layout is
highly non-portable. You might as well use GCC's extension of allowing
unsigned char bitfields.

Quote:
>And I imagined that if I create a structure like follwoing,

>struct some_register {
>        uchar_t r_wait:3,
>                r_intr:1,
>                r_dma:1,
>                r_port:3;
>};

The GNU compiler supports this as an extension, but warns if you use the -ansi
flag. It so happens that GCC is able to produce a structure of size 1 if all
it contains is an unsigned char bitfield. However, don't expect this to be
portable to other compilers.

Quote:
>them I can manipulate the reigster easily. Therfore your statement
>that the bitfield of a char are invalid ones makes me disappointed,
>and makes me complain to people who made ANSI-C.   I dare to think

It's noteworthy that C++ does not have this restriction, so maybe
you should consider compiling your program with g++. :)

Quote:
>that 'the bitfields of a char' are much much more useful ones than
>'those of an int'.  

That depends on what you mean by ``useful''.  In the broader context
of C programming, what is useful is that which is portable.  
There is no semantic advantage in declaring a member

        unsigned char x : 8;

versus

        unsigned x : 8;

Both of them have the same range of values, and there is no guarantee
that the non-portable variant will use less storage. If the C committee
changed the language to allow unsigned char to be a base type for a bit-field,
I doubt that they would impose additional requirements that  bitfields
of a smaller type should occupy less space than bitfields of a larger type.

You can see this usefulness of 'bitfields of

Quote:
>char' in the ip structure of /usr/include/netinet/ip.h.  You can

The implementation-supplied headers don't have to obey the rules of
the language.  They don't even have to be source files.

I could make the same argument about any GCC extension that is used
in Linux header files: see how useful it is to mark a non-returning
function with a suitable __attribute__ ((noreturn)) ? It should
be in the language!

All I see is a header file that fails to conform to the C language,
and which makes it potentially difficult to use some source code tools which
operate pre-processed output, and expect to be able to parse the output using
the ANSI C grammar and ANSI C constraint checks.

Quote:
>find a lot of examples at that directory if you are unsing a
>unix machine.

Which types of UNIXes have you investigated for this?

Quote:
>Is there anybody who has ever made the 'bitfields'
>of an int? I have never met a situation in which the bitfields of
>int might be useful ones because I can easily split up an integer
>into 'char, or short', and I have never met a source code that was
>coded using the 'bitfields of int'.

The idea behind bit-fields is that adjacent bit-fields may be packed
together to save space. For the purpose of packing, the type is largely
irrelevant; the type specifier serves only to constrain the size.
It is the bit field size that is important.

ANSI C makes it a constraint violation if the bit field width exceeds
the size of the bit-field's type, even if that type is other than int.

I've used bitfields of unsigned int type quite frequently, for example to save
space in representing various object flags:

        struct connection {
                unsigned allocated : 1;
                unsigned connected : 1;
                unsigned direction : 2;
        };

Such declarations are written in the hope that the bitfields will be packed
into a single unit of storage to save space, at the possible cost of access
time.  The size of the unit of storage is implementation-defined, and as such
is of little interest in portable coding situations.

In GCC, the _only_ advantage you gain by using an unsigned char bitfield
is that the compiler will use a smaller unit size for allocating the
bitfields, so that accessing a bitfield causes a single byte memory
transfer, rather than a word memory transfer (on machines that support
byte transfers, of course).

There are some additional things you might want to know about bitfields.

You can have unnamed bitfields:

        unsigned : 2;

These serve the purpose of padding. You can also have unnamed bitfields
of size zero:

        unsigned : 0;

The purpose of these is to force the _next_ bitfield to lie in a new
storage unit rather than be packed into the previous storage unit.

Also, it is implementation-defined whether a bitfield can straddle two
adjacent storage units. For example, if the storage unit is 16 bits
wide, it is implementation defined whether

        unsigned a : 15;
        unsigned b : 3;

will place the b into the next storage unit, or whether it will put one bit
of it into the first storage unit, and two bits into the next. However,
if you write

        unsigned a : 15;
        unsigned : 0;
        unsigned b : 3;

you can be sure that the b will go into the new storage unit.



Mon, 21 Aug 2000 03:00:00 GMT  
 A question about a structure containing ':'


Quote:


>> int', the existence of 'an unsigned char' is natural one.  For me
>> the bitfields of char is very important because I have to control
>> each bit pattern of a register in a board.   For example, for
>> the following a register, I am in a situation of controlling
>> each bit at my will,

>So just use the unsigned int bit fields, and ignore the top
>24 bits (or bottom 24, depending on endianess)!

You can't do this if you are accessing hardware, because there may be
a hardware requirement that the memory access be to a single byte.

Of course, if you are worried about things like that, you are no longer
writing in portable C.

The only thing you can portably do with bitfields is to use them as small
integral types in an attempt to save space.



Mon, 21 Aug 2000 03:00:00 GMT  
 
 [ 25 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Dialog containing a CTabCtrl containing a modeless dialog box, shortcuts don't work

2. Tooltip text containing '\t'failed when updating IE4 to IE5

3. newbie question - program's structure

4. ServicedComponent's - structure and attribute tagging question

5. Trouble with the 'FILE' structure

6. Global Structure doesn't retain it's value

7. 'Where's the C question?'

8. 'Object' does not contain a definition for 'Clone'

9. Marshaling a structure containing a aray of different structures

10. Program that doesn't contain the main() function

11. GetSchemeTable() doesn't always contain valid information?

12. free()'ing structs containing pointers to memory?

 

 
Powered by phpBB® Forum Software