git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Antti-Juhani Kaijanaho <antti-juhani@kaijanaho.info>
To: Linus Torvalds <torvalds@osdl.org>
Cc: Jason Riedy <ejr@EECS.Berkeley.EDU>,
	Junio C Hamano <junkio@cox.net>,
	git@vger.kernel.org
Subject: Re: [PATCH] Spell __attribute__ correctly in cache.h.
Date: Sun, 28 Aug 2005 22:08:53 +0300	[thread overview]
Message-ID: <43120BC5.8060608@kaijanaho.info> (raw)
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

  reply	other threads:[~2005-08-28 19:09 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-08-19  4:10 [PATCH] Spell __attribute__ correctly in cache.h Jason Riedy
2005-08-19  9:04 ` Junio C Hamano
2005-08-19 14:58   ` Jason Riedy
2005-08-19 19:53     ` Junio C Hamano
2005-08-23 21:20       ` Jason Riedy
2005-08-28 10:14         ` Junio C Hamano
2005-08-28 17:11           ` Jason Riedy
2005-08-28 17:46             ` Linus Torvalds
2005-08-28 19:08               ` Antti-Juhani Kaijanaho [this message]
2005-08-28 19:48                 ` Linus Torvalds
2005-08-29  8:17                   ` Martijn Kuipers
2005-08-29  8:35                     ` Antti-Juhani Kaijanaho
2005-08-29  8:55                       ` Martijn Kuipers

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=43120BC5.8060608@kaijanaho.info \
    --to=antti-juhani@kaijanaho.info \
    --cc=ejr@EECS.Berkeley.EDU \
    --cc=git@vger.kernel.org \
    --cc=junkio@cox.net \
    --cc=torvalds@osdl.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).