Align a POD srtuct on the stack (aliasing issue with gcc 4.6+)
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[]; }; 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 I have a huge function that allocates multiple aligned arrays on the stack using this approach and now it doesn't produce correct results with GCC 4.6+ (on arm, I didn't test it on x86). I've spent 2 days trying to fix the issue, perhaps gcc mailing list is a good place to ask for appropriate workaround? I want to ensure that the function produces correct result even with strict-aliasing enabled. Thanks.
RE: Align a POD srtuct on the stack (aliasing issue with gcc 4.6+)
> > 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[]; > }; > > 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 > > > I have a huge function that allocates multiple aligned arrays on the stack > using this approach and now it doesn't produce correct results with GCC > 4.6+ (on arm, I didn't test it on x86). > I've spent 2 days trying to fix the issue, perhaps gcc mailing list is a > good place to ask for appropriate workaround? I want to ensure that the > function produces correct result even with strict-aliasing enabled. > I also asked this question on SO, there are more details about the issue: http://stackoverflow.com/questions/14170010/strict-aliasing-and-memory-alignment
For contribution and participation
Hello, I am PRACHURYA PRAN SARMA from India, I am a first year Computer science and engineering student and I want to contribute and work in the lovely organisation. I know c language and I am learning c++ too. I am also going to participate in GSoC 2021. Thank you.