In the case of bit fields, which one is better to use, unsigned char or unsigned int and why?

I just want to know about the following structure declarations. Which one is better to use for memory allocation and why? What about padding in the case of unsigned char and unsigned int?

struct data{ unsigned char a:3; unsigned char b:4; }; 

and

 struct data{ unsigned int a:3; unsigned int b:4; }; 
+6
source share
3 answers

Bit fields must be declared with type signed int , unsigned int . Other types may or may not be supported.

From Atmel

in the C Standard, only "unsigned (int)" and "int" are acceptable datatypes for a bitfield member. Some compilers allow "unsigned char" .......

+7
source

C99 standard (§6.7.2.1 # 4)

The bit field must be of a type that is a qualified or unqualified version of _Bool , signed int , unsigned int, or some other type of implementation.

If the actual specifier of type int or typedef-name , defined as int , is used, then this is determined by the implementation whether the bit field is signed or unsigned .

(§6.7.2.1 # 15)

There may be an unnamed pad at the end of the structure or union.

An implementation can allocate any addressable storage block large enough to hold a bitfield.

Next (§6.7.2.1 # 11)

Declaring a bit field without a declarator, but only a colon and width, indicates an unnamed bit field. As a special case, an element of the structure of a bit field with a width of 0 indicates that further the bit field should be packed into a block in which the previous bit, if any, was placed.

An unnamed bit field structure element is useful for filling to match the external overlay layouts.

+5
source

As Amogh (categorically) and PHIfounder point out, the only fully portable types are _Bool , signed int and unsigned int . However, many compilers allow other integral types to combat bit-field packaging. In practice, bit fields are often used to represent device registers, where typically each bit or set of bits has its own meaning. The bit packing is dictated by 6.7.2.1 ad 11 standard C

An implementation can allocate any addressable storage block large enough to hold a bitfield. If there is enough space left, a bit field that immediately follows another bit field in the structure should be packed into adjacent bits of the same block. If there is not enough space, then whether a bit field that does not fit will fit in the next block or overlap adjacent units is determined by the implementation. The order of distribution of bit fields within a unit (from high order to low or low order) is determined by the implementation. The alignment of the storage address block is not indicated.

Many compilers have agreed that the “address storage unit” is of the type specified in the source. For example, the gcc compiler does not allow a 9-bit bit field of type unsigned char , but it does allow it for type unsigned int . In your examples, the gcc compiler for the Pentium structure using unsigned char will have a size of 1 byte, and the one that uses an unsigned int will be 4 bytes. Many compilers also agreed that if a bit field does not fit, it will not overlap with the next block. However, this can be applied using a bit field of width 0, which is defined by 6.7.2.1 ad 12 standard

Declaring a bit field without a declarator, but only a colon and a width, indicates an unnamed bit field. As a special case, an element of the structure of a bit field with a width of 0 indicates that the additional bit field should not be packed into the device in which the previous bit field was placed, if any.

If you mix bit fields with non-bit fields, then 6.7.2.1 ad 15 dictates that the address units of the bit fields and non-bit fields will have different addresses

Inside the structure object, the members of the non-bit field and the units in which the bit fields are located have addresses that increase in the order in which they are declared. A pointer to a structure object, appropriately transformed, points to its initial member (or if this element is a bit field, and then to the block in which it is located) and vice versa. There may be an unnamed addition to the structure object, but not at the beginning.

Some application architectures Binary interface (ABI) architectures provide the implementation of certain implementation options to ensure compatibility between different compilers for this architecture.

+2
source

Source: https://habr.com/ru/post/949526/


All Articles