[Bug c++/38455] New: aligned struct members in heap-allocated code

tim at klingt dot org gcc-bugzilla@gcc.gnu.org
Tue Dec 9 17:02:00 GMT 2008


the following code works on x86_64 as 64-bit binary, but the alignment
constraints of heap-allocated structs are not matched, when compiling 32-bit
code:

#include <assert.h>

template <int size = 4>
struct aligned_buffer
{
    char padding;
    float data[size] __attribute__((aligned((16))));
};

struct my_struct
{
    aligned_buffer<> buf1;
    char padding;
    aligned_buffer<> buf2;
};



int main()
{
    {
        aligned_buffer<> ab;
        assert((long)&ab.data % 16 == 0);
    }

    {
        my_struct m;
        assert((long)&m.buf1.data % 16 == 0);
        assert((long)&m.buf2.data % 16 == 0);
    }

    {
        aligned_buffer<> * ab = new aligned_buffer<>();
        assert((long)&ab->data % 16 == 0); /* fails */
    }

    {
        my_struct * m = new my_struct();
        assert((long)&m->buf1.data % 16 == 0); /* fails */
        assert((long)&m->buf2.data % 16 == 0); /* fails */
        delete m;
    }
}

g++ -m32: int main(): Assertion `(long)&ab->data % 16 == 0' failed.
g++ -m64: works fine

when changing the alignment from 16 to 64, the alignment constraints for
heap-allocated structs fail with both -m32 and -m64.


-- 
           Summary: aligned struct members in heap-allocated code
           Product: gcc
           Version: 4.3.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: tim at klingt dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=38455



More information about the Gcc-bugs mailing list