Struct member & __attribute__((aligned)) in public header
Ian Pilcher
arequipeno@gmail.com
Tue Jan 21 06:46:00 GMT 2014
I have a struct that uses a flexible array member to store values of
application-defined types. Since the flexible array member is defined
as an array of unsigned char, I am using __attribute__((aligned)) to
give it a reasonable alignment.
struct generic_value {
const char *name;
const struct value_type *type;
unsigned char flags;
unsigned char value[] __attribute__((aligned));
};
This is all well and good, but the definition of this struct may have to
become part of the public API of this library. Obviously, it would be
very, very bad if an application and the library had different ideas
about the alignment of the 'value' member.
I found this discussion from 2009:
http://comments.gmane.org/gmane.comp.gcc.devel/102830
Unfortunately, that thread doesn't really discuss the "best practices"
of a shared library in this situation. I see a few possibilities:
1) This isn't actually a problem. It was fixed back in '09, and I can
use __attribute__((aligned)) freely.
2) Use something like __attribute__((aligned(sizeof(void *)))). I'm
not writing performance-critical code, so this should really be
more than adequate.
3) Use offsetof to capture the offset of the value element at the time
my library is compiled (in a const size_t) and provide some sort of
macro/inline function to retrieve it in a "safe" manner -- along
with comments in the header file about the dangers of accessing it
directly.
Any thoughts on what the best approach is?
Thanks!
--
========================================================================
Ian Pilcher arequipeno@gmail.com
Sent from the cloud -- where it's already tomorrow
========================================================================
More information about the Gcc-help
mailing list