News for hackers. Stuff that matters.
Re:Humans should think of sizeof() as a function, says Linus... by xamuel on Saturday May 30, 2015 @09:25AM (#9630088)
This is a really great example why you SHOULDN'T think of sizeof as a function. If sizeof were a function, the code

  int *foo = NULL;
  foo = malloc(sizeof(*foo));
would be undefined behavior (dereferencing NULL)!
Re:Humans should think of sizeof() as a function, says Linus... by Kranar on Saturday May 30, 2015 @01:10PM (#9630831)
Dereferencing a null pointer is legal in C. It's the conversion of a null pointer from from an r-value to an l-value that's illegal, which does not happen in that snippet of code.

That's why it's perfectly legal in C to do this (&*foo), even if foo is a null pointer.

Re:Humans should think of sizeof() as a function, says Linus... by masklinn on Saturday May 30, 2015 @01:40PM (#9630950)
> Dereferencing a null pointer is legal in C. It's the conversion of a null pointer from from an r-value to an l-value that's illegal, which does not happen in that snippet of code.

And it does not happen in that snippet of code because sizeof is nothing like a function.