Bit field's memory management in C

后端 未结 3 1279
一整个雨季
一整个雨季 2021-01-18 10:50

To understand the bit field memory storage, I have created the test program below.

#include 

int main()
{
    int a;
    typedef struct {
            


        
3条回答
  •  终归单人心
    2021-01-18 11:20

    bit fields are non portable and machine dependent.

    Limitations of using bit fields

    When using bit fields, be aware of the following issues:

    1. The code will be non-portable since the organization of bits-within-bytes and bytes-within-words is machine dependent.
    2. You cannot take the address of a bit field; so the expression &mystruct.x is illegal if x is a bit field identifier, because there is no guarantee that mystruct.x lies at a byte address.
    3. Bit fields are used to pack more variables into a smaller data space, but causes the compiler to generate additional code to manipulate these variables. This costs in terms of code size and execution time.

提交回复
热议问题