linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Horst von Brand <vonbrand@inf.utfsm.cl>
To: Alan Cox <alan@lxorguk.ukuu.org.uk>
Cc: Andrew Morton <akpm@osdl.org>,
	Russell King <rmk+lkml@arm.linux.org.uk>,
	penberg@cs.Helsinki.FI, viro@ftp.linux.org.uk,
	linux-kernel@vger.kernel.org, torvalds@osdl.org
Subject: Re: p = kmalloc(sizeof(*p), )
Date: Tue, 20 Sep 2005 15:41:06 -0400	[thread overview]
Message-ID: <200509201941.j8KJf6hO002742@laptop11.inf.utfsm.cl> (raw)
In-Reply-To: Message from Alan Cox <alan@lxorguk.ukuu.org.uk>  of "Tue, 20 Sep 2005 19:02:41 +0100." <1127239361.7763.3.camel@localhost.localdomain>

Alan Cox <alan@lxorguk.ukuu.org.uk> wrote:
> On Maw, 2005-09-20 at 10:11 -0700, Andrew Morton wrote:
> > Russell King <rmk+lkml@arm.linux.org.uk> wrote:
> > >  Since some of the other major contributors to the kernel appear to
> > >  also disagree with the statement, I think that the entry in
> > >  CodingStyle must be removed.

> > Nobody has put forward a decent reason for doing so.  

> I've seen five decent reasons so far. Which of the reasons on the thread
> do you disagree with and why ?

Not sure that I'm following the logic here, so...

For one, I leaned towards "p = malloc(sizeof(*p))" before, but the reasons
given for "p = malloc(sizeof struct foo))" (or even "p = (struct foo *)
malloc(sizeof(struct foo))", wrapped in a macro) did convince me.

The gains for a reader/maintainer/code auditor I see:

- It is easier to find it later
- Initialization of *p should be nearby, finding it by type name is useful
  for checking/updating
- It forces you to think a bit before typing it in, this should make making
  mistakes somewhat harder

The loss for a code writer are:

- (Marginally) more typing
- Have to know the type of *p [but if you don't, better don't touch it...]

If the writer has got the type wrong, she will initialize wrongly (and the
compile will blow up), so I don't see any advantage. The only other case
would be something like:

   p = malloc(sizeof(...));
   memset(p, v, sizeof(...));

As v is more often than not 0, this should really be:

   p = calloc(1, sizeof(...));

and perhaps in this case (with /no/ further initialization) it could be
called a tie. For uniformity's sake I'd prefer "sizeof(struct foo)"
everywhere.

In any case, give me help in finding bugs and updating code over (minor)
initial coding convenience everyday.

In any case, as the parallel flamewars conclusively demonstrate, writing it
down in CodingStyle won't make everybody agree on using it anyway, so I'd
vote for including the "sizeof(struct foo)" version there as recommended
practice.
-- 
Dr. Horst H. von Brand                   User #22616 counter.li.org
Departamento de Informatica                     Fono: +56 32 654431
Universidad Tecnica Federico Santa Maria              +56 32 654239
Casilla 110-V, Valparaiso, Chile                Fax:  +56 32 797513

      parent reply	other threads:[~2005-09-20 19:42 UTC|newest]

Thread overview: 49+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-09-18 10:06 p = kmalloc(sizeof(*p), ) Russell King
2005-09-18 11:04 ` Alan Cox
2005-09-18 14:39   ` Al Viro
2005-09-18 16:25     ` Denis Vlasenko
2005-09-18 17:30       ` Al Viro
2005-09-18 18:00         ` Willy Tarreau
2005-09-18 17:47       ` Alan Cox
2005-09-18 16:32 ` Robert Love
2005-09-18 16:52   ` Willy Tarreau
2005-09-18 17:18     ` Al Viro
2005-09-18 17:31       ` Linus Torvalds
2005-09-18 17:45         ` Al Viro
2005-09-18 20:34           ` Roman Zippel
2005-09-18 21:12             ` Al Viro
2005-09-18 21:52               ` Al Viro
2005-09-18 22:25                 ` Linus Torvalds
2005-09-18 23:07                   ` Al Viro
2005-09-20  6:31                     ` Richard Henderson
2005-09-19 21:20                   ` Matthias Urlichs
2005-09-19 21:28                     ` Matthias Urlichs
2005-09-18 19:07         ` Al Viro
2005-09-18 21:30           ` Alan Cox
2005-09-18 21:14             ` Al Viro
2005-09-19  6:09             ` Coywolf Qi Hunt
2005-09-21  2:18         ` Miles Bader
2005-09-18 17:32   ` Randy.Dunlap
2005-09-19  6:47   ` Coywolf Qi Hunt
2005-09-20  8:53   ` Pekka Enberg
2005-09-20  9:39     ` Al Viro
2005-09-20  9:47       ` Pekka J Enberg
2005-09-20  9:53         ` Al Viro
2005-09-20 10:07           ` Pekka J Enberg
2005-09-20 15:14         ` Randy.Dunlap
2005-09-20 11:18 ` Pekka Enberg
2005-09-20 11:40   ` Russell King
2005-09-20 11:56     ` Denis Vlasenko
2005-09-20 12:20     ` Pekka J Enberg
2005-09-20 12:31       ` Russell King
2005-09-20 12:35         ` Pekka J Enberg
2005-09-20 15:21           ` Randy.Dunlap
2005-09-20 12:53         ` Pekka J Enberg
2005-09-20 17:11         ` Andrew Morton
2005-09-20 17:17           ` Russell King
2005-09-20 18:02           ` Alan Cox
2005-09-20 17:59             ` Andrew Morton
2005-09-20 18:11               ` Russell King
2005-09-20 18:41                 ` Jeff Garzik
2005-09-20 20:41               ` Alan Cox
2005-09-20 19:41             ` Horst von Brand [this message]

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=200509201941.j8KJf6hO002742@laptop11.inf.utfsm.cl \
    --to=vonbrand@inf.utfsm.cl \
    --cc=akpm@osdl.org \
    --cc=alan@lxorguk.ukuu.org.uk \
    --cc=linux-kernel@vger.kernel.org \
    --cc=penberg@cs.Helsinki.FI \
    --cc=rmk+lkml@arm.linux.org.uk \
    --cc=torvalds@osdl.org \
    --cc=viro@ftp.linux.org.uk \
    /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).