News for hackers. Stuff that matters.
Re:Humans should think of sizeof() as a function, says Linus... by lazzlazzlazz on Saturday May 30, 2015 @06:47AM (#9629716)
Who the hell doesn't think of `sizeof` as a function?
Re:Humans should think of sizeof() as a function, says Linus... by noselasd on Saturday May 30, 2015 @07:23AM (#9629796)
It's an unary operator, with 2 forms:

   sizeof(type)
or

  sizeof expression
but e.g. unlike a function, it doesn't evaluate the expression.

    sizeof(my_function()) doesn't call my_function.

    sizeof(a = 12) doesn't assign 12 to a.
Re:Humans should think of sizeof() as a function, says Linus... by lazzlazzlazz on Sunday May 31, 2015 @05:00PM (#9635520)
That's a language design choice - not a conceptual argument.
Re:Humans should think of sizeof() as a function, says Linus... by Retr0spectrum on Saturday May 30, 2015 @07:40AM (#9629829)
So is "sizeof(a = 12)" equivalent to just "sizeof(a)"?
Re:Humans should think of sizeof() as a function, says Linus... by hebdo on Saturday May 30, 2015 @08:11AM (#9629906)
Well, `a` could overload the "=" operator, in which case you would get `sizeof(a.operator=(12))`. But as far as pure C goes I believe you are right.

Edit: Via the C99 spec (http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf).

The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer. If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant.

The type of an assignment expression is the type of the left operand unless the left operand has qualified type, in which case it is the unqualified version of the type of the left operand.

So it is indeed the case that `sizeof(a = 12)` is equivalent to `sizeof(a)` in the C world.

Re:Humans should think of sizeof() as a function, says Linus... by to3m on Saturday May 30, 2015 @09:05AM (#9630047)
An interesting result of this (I suppose...) is that if a macro expands an argument twice, and one of those times is an operand for sizeof, you don't need to mention the double expansion in the documentation.
Re:Humans should think of sizeof() as a function, says Linus... by Luc on Saturday May 30, 2015 @06:52AM (#9629727)
I learned C from 'Kernighan & Ritchie', where it's never referred to as a function but as a compile-time operator. I've never thought of it as a function.
Re:Humans should think of sizeof() as a function, says Linus... by _lce0 on Saturday May 30, 2015 @07:05AM (#9629758)
Well it's an operator, and algebra said all them are just functions... Just like `+` is a function

Anyways not all those writing code have CS background

Re:Humans should think of sizeof() as a function, says Linus... by weland on Saturday May 30, 2015 @08:11AM (#9629904)
Except algebra said that about algebraic functions and operators.

I know a CS major who insisted in pointing out the same thing. He was extremely surprised when dragons hatched from his eggs instead of the chickens he expected. It was a really heated event. He barely escaped -- and then ranted for several weeks how they were really just eggs and couldn't explain what happened.

Re:Humans should think of sizeof() as a function, says Linus... by masklinn on Saturday May 30, 2015 @07:26AM (#9629801)
Except sizeof is a compile-time operation, not runtime. If sizeof were a function,

    int *foo = malloc(sizeof(*foo));
would make no sense.
Re:Humans should think of sizeof() as a function, says Linus... by barrkel on Saturday May 30, 2015 @08:14AM (#9629910)
Compile-time functions are a thing. Not in C, but in other languages, including C++.

This all comes down to what you think "function" means. If you're living in a C bubble, of course sizeof isn't a function. If you have a wider mapping (!) for the word, then it is a kind of function.

Re:Humans should think of sizeof() as a function, says Linus... by mcguire on Saturday May 30, 2015 @11:30AM (#9630468)
If you think everything called a "function", in every context, is the same thing, you are going to have a very bad day.
Re:Humans should think of sizeof() as a function, says Linus... by userbinator on Saturday May 30, 2015 @09:15AM (#9630064)
In C99, sizeof is compile-time only if VLAs are not involved.

In C89, sizeof is always compile-time.

Re:Humans should think of sizeof() as a function, says Linus... by rdc12 on Saturday May 30, 2015 @07:44AM (#9629843)
So in practice it works more like a C Macro then a C function?

Edit, except it doesn't treat arguments the same, so no.

Re:Humans should think of sizeof() as a function, says Linus... by cygx on Saturday May 30, 2015 @09:21AM (#9630080)
It's neither:

sizeof belongs in its own category of unary compile-time(-ish) operators, whose only other member is _Alignof.

If you're so inclined, you might add _Generic and _Pragma to that mix as well, but they aren't really a close fit.