Some subsurface notes from a week of diving

Linus Torvalds torvalds at linux-foundation.org
Mon Mar 17 08:31:05 PDT 2014


On Mon, Mar 17, 2014 at 8:13 AM, Dirk Hohndel <dirk at hohndel.org> wrote:
>
> Look at this little gem. I think I'll reorder this a little bit:

No need.

> +static inline int number_of_computers(struct dive *dive)
> +{
> +       int total_number = 0;
> +       struct divecomputer *dc = &dive->dc;
> +
> +       if (!dive)
> +               return 1;
>
> (hint: think what happens if dive == null)

Absolutely nothing happens.

It does not *access* the dive pointer. It just creates a new pointer
*offset* from the dive pointer.

Doing "&ptr->xyz" is just pointer arithmetic. It may syntactically
look like an access due to the "->", but it really isn't. It literally
means "calculate the address of the field xyz in the structure pointed
to by ptr".

[ Same goes for "&*ptr" btw: the "&" operator literally "undoes" the
access that is implied by the '*', and the result is just "ptr", with
no implied access, so you can do it even on a NULL pointer ]

                Linus


More information about the subsurface mailing list