In our company it is very common to send a C/C++ structures over a network. Any struct has one or many uint32_t fields:
typedef struct {
uint32_t data1;
uint32_t data2;
} Data;
and fields (data1, data2, ...) encapsulates the real data we need to send. Our data is mostly of a single or few bits long, so to save space/bandwidth it is aligned inside those fields in a specific bit position.
To access our real data we write (outside of a struct!) a 'getters' and 'setters' macros with bitshifting and bit masking:
#define READY_MASK 0x01 // indicates that READY is a single bit value
#define READY_OFFSET 3 // indicates position of READY inside 32-bit field
#define IS_READY(x) { ... } // returns READY value from x field
#define SET_READY(x,r) { ... } // sets READY value to x field
Now I'd like to modify, simplify and make this process more secure by adding getters and setters directly into the structure, for example:
typedef struct {
uint32_t data1;
#define READY_MASK 0x01
#define READY_OFFSET 3
inline void set_ready(uint32_t r) { /*...*/ }
inline uint32_t is_ready() { /*...*/ }
// lots of other getters and setters
} Data1;
As far as my experiments are correct, I've noticed that this kind of modification doesn't affect in a size of structure sizeof(Data)==sizeof(Data1)
, I can send this structure over the network and receive and decode on the other end.
My question is: is there anything incorrect in this modification? Is there anything risky or anything that I should be aware of?