LLVM Bugzilla is read-only and represents the historical archive of all LLVM issues filled before November 26, 2021. Use github to submit LLVM bugs

Bug 5637 - Example where sizeof differs between clang and gcc
Summary: Example where sizeof differs between clang and gcc
Status: RESOLVED FIXED
Alias: None
Product: clang
Classification: Unclassified
Component: Frontend (show other bugs)
Version: unspecified
Hardware: PC Linux
: P normal
Assignee: Unassigned Clang Bugs
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-11-29 14:50 PST by Duncan Sands
Modified: 2011-04-26 16:06 PDT (History)
5 users (show)

See Also:
Fixed By Commit(s):


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Duncan Sands 2009-11-29 14:50:14 PST
Clang outputs 72 and gcc 80 on my x86-64 linux box.  What's going on here
is that gcc does not round up the size of the type UINT192 to the alignment
the user specified, but it does use this alignment to round up the size of
the array of UINT192.  I asked on #gcc whether this should be considered a
gcc bug, but richi thought it was "by design".

#include <stdio.h>

typedef __attribute__((aligned(16))) struct {
  unsigned long long w[3];
} UINT192;

UINT192 ten2mk192M[] = {
    {{0xcddd6e04c0592104ULL, 0x0fcf80dc33721d53ULL, 0xa7c5ac471b478423ULL}},
    {{0xcddd6e04c0592104ULL, 0x0fcf80dc33721d53ULL, 0xa7c5ac471b478423ULL}},
    {{0xcddd6e04c0592104ULL, 0x0fcf80dc33721d53ULL, 0xa7c5ac471b478423ULL}}
};

int main(void) {
  printf("sizeof: %lu\n", sizeof(ten2mk192M));
  return 0;
}
Comment 1 Argyrios Kyrtzidis 2011-01-28 23:15:29 PST
I'm not so sure that this is a clang bug.

I can't find any mention in the standards about padding in arrays. C++ 5.3.3p2 (expr.sizeof) says:
"When applied to an array, the result is the total number of bytes in the array. This implies that the size of an array of n elements is n times the size of an element."

C99 6.5.3.4p6 (sizeof) implies the same with "Another use of the sizeof operator is to compute the number of elements in an array: sizeof array / sizeof array[0]"

Note that in both standards, in those sections they specifically mention padding for classes/structures.

You lose the symmetry in multidimensional arrays:
sizeof(UINT192[2][3]) == 160
sizeof(UINT192[3][2]) == 144

Could someone let me know what is the justification for the padding in the array ?
Comment 2 Argyrios Kyrtzidis 2011-01-31 15:47:51 PST
Submitted http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47557 to get input from gcc developers.
Comment 3 Chris Lattner 2011-03-10 22:53:38 PST
Reverified, amusingly llvm-gcc ICE's on this.
Comment 4 Duncan Sands 2011-03-11 01:30:00 PST
Dragonegg doesn't though :)
Comment 5 Argyrios Kyrtzidis 2011-04-26 16:06:23 PDT
Fixed at r130242.