Some subsurface notes from a week of diving

Thiago Macieira thiago at macieira.org
Mon Mar 17 10:56:58 PDT 2014


Em seg 17 mar 2014, às 08:31:05, Linus Torvalds escreveu:
> > +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.

Actually, the compiler is allowed in this case to assume that dive is never 
null.

Doesn't matter that you didn't actually dereference the memory location. It's 
still undefined behaviour to do what you've done.

The offsetof macro notwithstanding, since that is provided by the compiler and 
can be implemented through a builtin. And it is:

$ gcc -dM -E -include stddef.h -xc /dev/null | grep offsetof
#define offsetof(TYPE,MEMBER) __builtin_offsetof (TYPE, MEMBER)

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel Open Source Technology Center
      PGP/GPG: 0x6EF45358; fingerprint:
      E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358



More information about the subsurface mailing list