This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Align a POD srtuct on the stack (aliasing issue with gcc 4.6+)
- From: "H.J. Lu" <hjl dot tools at gmail dot com>
- To: "pps ." <mtlroom at hotmail dot com>
- Cc: "gcc at gcc dot gnu dot org" <gcc at gcc dot gnu dot org>
- Date: Sun, 6 Jan 2013 09:04:11 -0800
- Subject: Re: Align a POD srtuct on the stack (aliasing issue with gcc 4.6+)
- References: <BAY002-W98EEBA886EC80FDF07370FA5260@phx.gbl>
On Sat, Jan 5, 2013 at 7:00 PM, pps . <mtlroom@hotmail.com> wrote:
> Question: How can I allocate random amount of stack space (using char
> arrays or alloca, and then align pointer to that stack space and
> reinterpret this chunk of memory as some structure that has some well
> defined layout that guarantees alignment of certain variables as long
> as the structure itself is aligned properly? How can I do so with 4.6+
> GCC with full optimizations enabled with strict aliasing enabled (e.g.
> without passing -fno-strict-aliasing).
>
> Pseudo code:
>
> struct my_array
> {
> char data[6666];
> };
>
> void * buffer = alloca(sizeof(my_array) + 32);
> void * buffer32 = (((uintptr_t)buffer) + 31) & (~31);
> assert( ((uintptr_t)buffer) % 32 == 0);
>
> my_array * data = (my_array*)buffer32;
>
> .... now use my_array, data->data is 32-byte aligned
>
Why don't you let GCC to align struct my_array to
32 byte for you? You can do
struct my_array data __attribute__((aligned(32)));
--
H.J.