linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Trevor Woerner <twoerner@gmail.com>
To: Hubert CHAUMETTE <hubert.chaumette@wanadoo.fr>,
	Daniel Hilst Selli <danielhilst@gmail.com>,
	"linux-c-programming@vger.kernel.org"
	<linux-c-programming@vger.kernel.org>
Subject: Re: sizeof *data, when data isn't initalized, is that right?
Date: Fri, 23 Jan 2015 02:33:20 -0500	[thread overview]
Message-ID: <54C1F940.5090400@gmail.com> (raw)
In-Reply-To: <54C143F3.7020704@wanadoo.fr>

On 01/22/15 13:39, Hubert CHAUMETTE wrote:
> As I understand it, sizeof(*data) is the same as sizeof(struct mcp23s08_driver_data). There should be no pointer dereference here, so I would say it isn't an issue.

In most cases (always?) sizeof() is a compile-time operator; it is up to
the compiler to perform the calculation of an object's size and simply
place that constant into the code. This is why the target of a sizeof()
operation needs to be fully defined at compile time.

Therefore, since this calculation takes place at compile time, there is
no run-time pointer dereference taking place.


>  That notation is quite confusing though.

This idiom is quite common, so it would be good to become familiar with
it. Without using this idiom one would have to write:

    sizeof (struct mpc23s08_driver_data)

which is fine... until someone else comes along and changes "data"'s
type (defined way up at the start of the function) to, say,
mpc23s08_driver_data_2. Then someone would need to go through all the
code and find all instances of

    sizeof (struct mpc23s08_driver_data)

and change all of them to

    sizeof (struct mpc23s08_driver_data_2)

As you might guess, this is prone to error. However, if the original
developer used

    sizeof (*data)

throughout their code, no such subsequent changes would be needed. The
compiler is smart enough to figure out that *data is an object of struct
mpc23s08_driver_data and use that in its sizeof calculation. If/when
"data"'s definition gets changed, the compiler is then able to use
mpc23s08_driver_data_2 in its calculation, without any necessary
subsequent changes to the code.

      reply	other threads:[~2015-01-23  7:33 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-22 18:19 sizeof *data, when data isn't initalized, is that right? Daniel Hilst Selli
2015-01-22 18:38 ` Hubert CHAUMETTE
2015-01-22 18:39 ` Hubert CHAUMETTE
2015-01-23  7:33   ` Trevor Woerner [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=54C1F940.5090400@gmail.com \
    --to=twoerner@gmail.com \
    --cc=danielhilst@gmail.com \
    --cc=hubert.chaumette@wanadoo.fr \
    --cc=linux-c-programming@vger.kernel.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).