I'm using GCC compiler for Microblaze processor. Recently I have encountered a problem with variable alignment. I've noticed that sometimes the compiler allocates static variable to an unaligned address (the address is not divisible by 4), so if I send a pointer of the unaligned variable to any function I can get an unaligned access hardware exception.
I have two questions regarding that subject:
How can I make sure all my static variables are aligned? is there a flag that forces that? Currently I'm using the variable attribute:
__attribute__((aligned(4)))
But this is very uncomfortable, because I need to define it for every static variable I have, which does not make sense.
- Is there a way to make sure my functions local variables (which are allocated in the stack) will be aligned? I mean is there a way to make sure my stack head is aligned, every function uses an aligned portion of the stack and any variable that is allocated in the stack is aligned.
Thanks.
_Alignas
specifier (resp.stdalign.h
names). You can check the alignment your compiler should expect with_Alignof
. I'd test the issue very thoroughly this before filing a bug report. – too honest for this site