Embedded Programmer
Embedded Programmer

Reputation: 641

struct alignment without argument and alignment on 16-bit target

I have some questions about struct alignment attributes without an argument. When declaring a structure with alignment like given below.

struct __attribute__ ((aligned))
{
     char a;
}s;
printf("%zu\n",sizeof(s));

output is 16.
according to following link. http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Type-Attributes.html

the compiler automatically sets the alignment for the type to the largest alignment which is ever used for any data type on the target machine you are compiling for.

It means I can't declare alignment greater than 16 byte. But if I give

struct __attribute__ ((aligned(32)))
{
     char a;
}s;
printf("%zu\n",sizeof(s));

then I am getting output 32.
It means we can declare greater than 16 byte alignment.
Then why are getting 16 byte? Is it compiler specific or target specific?

Alignment on 16 bit system — I read that there will not be any alignment issue on 16 bit system. Can anyone tell me why?

Upvotes: 1

Views: 245

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 754450

If the compiler gets an unqualified __attribute__((aligned)) attribute, it defaults to the largest necessary alignment on the machine. That is not the same as saying 'you cannot set a larger alignment'; your test demonstrates that you can set a larger alignment explicitly. You just won't get a bigger (or smaller) alignment unless you are explicit about it.

Upvotes: 2

Related Questions