[Bug c/24944] New: Size of type not multiple of its alignment.

neilt+bugs at chiark dot greenend dot org dot uk gcc-bugzilla@gcc.gnu.org
Sat Nov 19 18:03:00 GMT 2005


This topic was discussed in the following thread, but nothing seems to have
happened.  I couldn't find a bug report.
<http://gcc.gnu.org/ml/gcc/2003-09/msg01031.html>

The test case below demonstrates some problems with arrays of aligned types. 
Packing the aligned type in a structure fixes the problems.

Results:
FAIL: sizeof(foo_aligned)!=8  {7!=8}
FAIL: sizeof(foo_aligned_array)!=100*8  {700!=800}
FAIL: p2-p1!=3*8  {18!=24}

It appears that getting the address of x[3] rounds the size down to the
alignment, so this should be fixed by ensuring that the size is a multiple of
the alignment.

#include <stdio.h>
#include <stdlib.h>

typedef char foo[7];
typedef foo foo_aligned __attribute__((aligned(2)));
typedef struct { foo_aligned c; } foo_aligned_struct;
typedef foo_aligned foo_aligned_array[100];
typedef foo_aligned_struct foo_aligned_struct_array[100];

int result = 0;

void fail(const char *as, int a, const char *bs, int b)
{
  fprintf(stderr, "FAIL: %s!=%s  {%d!=%d}\n", as, bs, a, b);
  result = 1;
}

#define expect(A,B) \
  do { if((A)!=(B)) fail(#A, A, #B, B); } while(0)

int main()
{
  foo_aligned_array x;
  foo_aligned_struct_array y;
  char *p1, *p2;

  expect(sizeof(foo_aligned), 8);
  expect(sizeof(foo_aligned_struct), 8);
  expect(sizeof(foo_aligned_array), 100*8);
  expect(sizeof(foo_aligned_struct_array), 100*8);
  p1 = (char*)&(x[0]);
  p2 = (char*)&(x[3]);
  expect(p2-p1, 3*8);
  p1 = (char*)&(y[0]);
  p2 = (char*)&(y[3]);
  expect(p2-p1, 3*8);
  return result;
}


-- 
           Summary: Size of type not multiple of its alignment.
           Product: gcc
           Version: 4.0.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: neilt+bugs at chiark dot greenend dot org dot uk
 GCC build triplet: i486-linux-gnu
  GCC host triplet: i486-linux-gnu
GCC target triplet: i486-linux-gnu


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



More information about the Gcc-bugs mailing list