Redundant __packed__ attributes

This code is for the Microchip PIC32MX microprocessor. Their compiler is essentially GCC 3.4.

I try to use the GCC __packed__attribute to pack the bit fields into a union, and then restore them as unsigned char(i.e. type-punning) for sending via SPI or I2C. This behavior is determined by my implementation and works great. I prefer this over a hundred lines of masking and offset :)

My question is: are there any attributes __packed__in the code below that are redundant? At first glance, I think that those of the union members at the highest level can be dispensed with, but I'm not sure. Or can I leave them in a nested structure?

// Remember that bitfields cannot straddle word boundaries!
typedef struct
{
    /// Some flag #1
    unsigned FlagOne            : 1 __attribute__((packed));
    /// Some flag #2
    unsigned FlagTwo            : 1 __attribute__((packed));
    /// A chunk of data
    unsigned SomeData           : 5 __attribute__((packed));

    // and so on, maybe up to 32 bits long depending on the destination

} BlobForSomeChip;

/// This kind of type-punning is implementation defined. Read Appendix A (A7, A12) of
/// the MPLAB C Compiler for PIC32 MCUs manual.
typedef union
{
    /// Access the members of this union to set flags, etc
    BlobForSomeChip blobdata __attribute__((packed));

    /// As a byte for sending via SPI, I2C etc
    unsigned char bytes[4] __attribute__((packed));

} BlobData;
+3
1

, -Wall.

:

  • BlobForSomeChip 7 . 4 - , 1 .
  • A unsigned char[4] . 4 , , .

:

  • struct BlobForSomeChip= 1
  • unsigned char[4]= 4
  • BlobData= 4 ( ).

, BlobData . GCC , - . -Wall.

+2

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


All Articles