https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66661

Florian Weimer <fw at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |fw at gcc dot gnu.org

--- Comment #11 from Florian Weimer <fw at gcc dot gnu.org> ---
I think this is a GCC bug.

Object sizes do not have to be a multiple of the object alignment.  Consider
this example:

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

_Alignas (128) unsigned v0 = 128;
_Alignas (8) unsigned v1 = 8;
_Alignas (8) unsigned v2 = 8;
_Alignas (8) unsigned v3 = 8;
_Alignas (4) unsigned v4 = 4;
_Alignas (4) unsigned v5 = 4;
_Alignas (8) unsigned v6 = 8;
_Alignas (8) unsigned v7 = 8;
_Alignas (8) unsigned v8 = 8;
_Alignas (4) unsigned v9 = 4;
_Alignas (4) unsigned v10 = 4;

static int
cmp(const void *a, const void *b)
{
  unsigned *const *a1 = a;
  unsigned *const *b1 = b;
  if (*a1 < *b1)
    return -1;
  if (*a1 > *b1)
    return 1;
  return 0;
}

int
main()
{
  unsigned *p[] = {&v1, &v2, &v3, &v4, &v5, &v6, &v7, &v8, &v9, &v10};
  qsort (p, 10, sizeof (p[0]), cmp);
  for (int i = 0; i < 9; ++i) {
    printf ("%d: alignment %u, size %zd\n",
            i, *p[i], (p[i + 1] - p[i]) * sizeof (*p[i]));
  }
}

With gcc-5.3.1-6.fc23.x86_64, it prints this for me:

0: alignment 4, size 4
1: alignment 4, size 4
2: alignment 8, size 8
3: alignment 8, size 8
4: alignment 8, size 4
5: alignment 4, size 4
6: alignment 4, size 8
7: alignment 8, size 8
8: alignment 8, size 8

So the assumption that objects are at least as large as their alignment appears
to be refuted by code generated by GCC.

Reply via email to