4

I'm using this implementation of container_of on arm:

#define container_of(ptr, type, member)                 \
({                              \
    const typeof(((type *) NULL)->member) *__mptr = (ptr);  \
    (type *) (void*)((char *) __mptr - offsetof(type, member)); \
})

The problem is that not even adding (void*) cast to this implementation gets rid of the -Wcast-align warning. I really don't want to have to turn it off just because it fails in this one specific case where it is in fact ok. (the problem is that casting to char* and then to type* changes alignment of the type. A solution that has been used before is to first cast to void* and then to type* as a way of telling the compiler that it is intentional - but this solution does not work for me on gcc version 4.8.3 (OpenWrt/Linaro GCC 4.8-2014.04 r48666)

Is there a way to tell compiler to allow this cast just here?

Martin
  • 3,509
  • 3
  • 26
  • 31

0 Answers0