git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: sizeof(struct ...)

From
EMErik Mouw <erik@harddisk-recovery.com>
Date
Nov 23, 2006, 16:42 UTC
Message-ID
<20061123164258.GE6581@harddisk-recovery.com>
In-Reply-To
<4565C8F4.6000606@lsrfire.ath.cx>
On Thu, Nov 23, 2006 at 05:14:44PM +0100, Ren? Scharfe wrote:
Show 27 quoted lines
> Erik Mouw schrieb:
> > On Thu, Nov 23, 2006 at 04:45:09PM +0100, Ren? Scharfe wrote:
> >>  Is there really a compiler that inserts padding between arrays of
> >> unsigned chars?
> > 
> > Yes, that compiler is called "gcc".
> > 
> > #include <stdio.h>
> > 
> > struct foo {
> >         unsigned char a[3];
> >         unsigned char b[3];
> > };
> > 
> > int main(void)
> > {
> >         printf("%d\n", sizeof(struct foo));
> >         return 0;
> > }
> > 
> > On i386 that prints 6, on ARM it prints 8.
> 
> Does it add 1 byte after a and and 1 after b or two after b?
> I suspect it's the latter case -- otherwise Gerrit's patch,
> which started this thread, wouldn't help solve his problem.
> Or the pad sizing follows complicated rules that I do not
> understand at the moment.
You're right, it adds the padding after b:

#define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER) printf("%d %d\n", offsetof(struct foo, a), offsetof(struct foo, b));

prints "0 3" on ARM.
> Time to look for an ARM emulator, it seems.

objdump -D -S is your friend. I didn't have an ARM target ready, but at least I know enough ARM assembly that I can see what it will print :)

Erik
-- 
+-- Erik Mouw -- www.harddisk-recovery.com -- +31 70 370 12 90 --
Previous: René ScharfeNext: Junio C Hamano
Message 10 of 13 in “sizeof(struct ...)”
  1. Gerrit PapeNov 23, 2006
  2. René ScharfeNov 23, 2006
  3. René ScharfeNov 23, 2006
  4. Andy WhitcroftNov 23, 2006
  5. René ScharfeNov 23, 2006
  6. Erik MouwNov 23, 2006
  7. René ScharfeNov 23, 2006
  8. Andy WhitcroftNov 23, 2006
  9. René ScharfeNov 23, 2006
  10. Erik MouwNov 23, 2006
  11. Junio C HamanoNov 23, 2006
  12. archive-zip: don't use sizeof(struct ...)René Scharfe, Nov 23, 2006
  13. Gerrit PapeNov 24, 2006

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.