Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

_Alignas for struct members using clang & C11

I'm having some trouble with -Wpadded using C11 and structs.

I've already read Structure member alignment with _Alignas, and I looked in the clang docs and saw that it IS supported now.

Also, I'm using a very new version of clang that I built from trunk recently.

$ clang --version
clang version 3.3 (trunk 175473)
Target: x86_64-unknown-linux-gnu
Thread model: posix

The problem I'm running into is this:

#include <stdlib.h>
#include <stdalign.h>

struct foo{
   void* a;
   int b;
};

int main() {
   struct foo instance;

   instance.a = NULL;
   instance.b = 2;

   return 0;
}

Which throws me this warning:

$ clang -Weverything -std=c11 t.c 
t.c:4:8: warning: padding size of 'struct foo' with 4 bytes to alignment boundary [-Wpadded]
struct foo{
       ^
1 warning generated.

Now isn't this what _Alignas is for? I tried putting it before the int member declaration, like so:

struct foo{
   void* a;
   _Alignas(void*) int b;
};

But the same warning remains. I also tried putting the _Alignas in various places, to no avail. What am I missing here?

I know I could just ignore this particular warning and I understand why padding is important, so I'm not interested in workarounds or explanations about what padding is. I want to know how to change my C in a portable, standards conformant way so that the warning is no longer emitted.

like image 756
Max DeLiso Avatar asked Feb 22 '13 18:02

Max DeLiso


1 Answers

-Weverything prints all diagnostic messages required by C as well as some diagnostics not required by C. The diagnostic that is printed here is not required by C: its purpose is informative and your program is already strictly conforming. C says an implementation is free to produce additional diagnostic messages as long as it does not fail to translate the program.

like image 200
ouah Avatar answered Oct 08 '22 11:10

ouah