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

Re: [PATCH] Spell __attribute__ correctly in cache.h.

From
Antti-Juhani Kaijanaho <antti-juhani@kaijanaho.info>
Date
Aug 28, 2005, 19:08 UTC
Message-ID
<43120BC5.8060608@kaijanaho.info>
In-Reply-To
<Pine.LNX.4.58.0508281045060.3317@g5.osdl.org>
Linus Torvalds wrote:
> But using "array[]" means that "sizeof()" no longer works, and then you 
> have to use "offsetof()", which is a big pain.
This is not true under C99.  If an array[] is the last member of a
struct (which is what we are, AFAIK, talking about), then sizeof that
struct is defined and gives the size of that struct as if the array's
size were zero (but the struct cannot be used in an automatic context).
 Hence the idiom
  struct foo {
    ...
    int bar[];
  };
  ...
  struct foo *baz = malloc(sizeof *baz + 15 * sizeof (int));
  ...
which allocates baz in such a way that bar is a 15-element array of ints.

Of course, I cannot speak of how other non-C99 compilers implement this, but GCC gets it right:

ajk@kukkamaljakko:~$ cat foo.c #include <stdio.h>

struct foo {
        int a;
        int b[];
};
struct bar {
        int a;
        int b[1];
};
int main(void)
{
        printf("sizeof(struct foo) = %zu\n"
               "sizeof(struct bar) = %zu\n"
               "sizeof(int) = %zu\n",
               sizeof(struct foo),
               sizeof(struct bar),
               sizeof(int));
        return 0;
}
ajk@kukkamaljakko:~$ gcc --std=c89 -pedantic -Wall -W -ofoo foo.c
foo.c:5: warning: ISO C90 does not support flexible array members
foo.c: In function 'main':
foo.c:20: warning: ISO C90 does not support the 'z' printf length modifier
foo.c:20: warning: ISO C90 does not support the 'z' printf length modifier
foo.c:20: warning: ISO C90 does not support the 'z' printf length modifier
ajk@kukkamaljakko:~$ gcc --std=c99 -pedantic -Wall -W -ofoo foo.c
ajk@kukkamaljakko:~$ ./foo
sizeof(struct foo) = 4
sizeof(struct bar) = 8
sizeof(int) = 4
ajk@kukkamaljakko:~$
(Tested with both 3.3 and 4.0 in Debian unstable.)
-- 
Antti-Juhani
Previous: Linus TorvaldsNext: Linus Torvalds
Message 9 of 13 in “Spell __attribute__ correctly in cache.h.”
  1. Spell __attribute__ correctly in cache.h.Jason Riedy, Aug 19, 2005
  2. Junio C HamanoAug 19, 2005
  3. Jason RiedyAug 19, 2005
  4. Junio C HamanoAug 19, 2005
  5. Jason RiedyAug 23, 2005
  6. Junio C HamanoAug 28, 2005
  7. Jason RiedyAug 28, 2005
  8. Linus TorvaldsAug 28, 2005
  9. Antti-Juhani KaijanahoAug 28, 2005
  10. Linus TorvaldsAug 28, 2005
  11. Martijn KuipersAug 29, 2005
  12. Antti-Juhani KaijanahoAug 29, 2005
  13. Martijn KuipersAug 29, 2005

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.