in the gcc documentation they give information on how integers can be aligned. Can the same thing be done for packing integers?
For example, is this valid on a system that doesn't do automatically handle improperly aligned data?
typedef uint16_t __attribute__ ((packed)) packed_uint16_t;
On my system it gives:
align.c:7:1: warning: ‘packed’ attribute ignored [-Wattributes]
typedef uint16_t attribute ((packed)) packed_uint16_t;
But my system properly handles unaligned bytes, so I guess it would ignore it then!
This is a follow up to this question
The __attribute__((packed))
only pertains structures. It specifies that there shall be no padding between or after structure members. The compiler still assumes that the structure itself is aligned correctly but it might generate special code to access the misaligned structure members.
If you have a misaligned pointer (I'm not sure how you get such a pointer any way) and you want to portably dereference it, consider doing something like this:
some_type *mptr; /* misaligned pointer */
char *buffer = malloc(sizeof *mptr); /* get some aligned memory */
memcpy(buffer, mptr, sizeof *mptr);
some_type *aptr = (some_type*)mptr; /* pointer to aligned data */