Live data from Hacker News

Humans should think of sizeof() as a function, says Linus Torvalds

lkml.org

31–40 of 140 posts

Re: Humans should think of sizeof() as a function, says Linus Torvalds

#31
post #6

IIRC, if the argument to sizeof is a type, the parentheses are mandatory, anyway, so using it like it was a function is more consistent. I think the reason it is not a function from the standard's point of view is that C does not have any builtin functions (unless my memory totally fails me in this case), all functions have to be either defined locally or #included.

Another oddity of C that amuses me is the do/while loop without braces:

    int i = 4;
    do
       printf("hey\n");
    while (--i > 0);
Even though do/while is a keyword bracketing pair in C, it still only lets you use a single statement (because nested whiles). So everybody uses braces, and thus it looks quite disturbing without them.

Re: Humans should think of sizeof() as a function, says Linus Torvalds

#32
post #17

What kind of monsters don't use parentheses on sizeof. Is it the same guys that put { on new lines and have giant comment templates for every one line function.

Ah, to join the bikeshedding: { on newlines make imho more sense, the block of code is visually balanced that way. We have high-res screens now, no reason to pretend we're still on 80 char terminals.

But work long enough in either one of those styles, and the other one will start to look strange.

Re: Humans should think of sizeof() as a function, says Linus Torvalds

#33
post #30

Earlier quoted context omitted.

{ on new lines seems to have dominated the majority of code I've worked with although I've always preferred putting it on the same line myself.

I put it on the line that makes the code following it the most clear

How do you handle auto-format? If a rules doesn't survive that, its not practical, at least for me.

Re: Humans should think of sizeof() as a function, says Linus Torvalds

#34
post #7

Who the hell doesn't think of `sizeof` as a function?

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

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 Torvalds

#35

Earlier quoted context omitted.

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.

So is "sizeof(a = 12)" equivalent to just "sizeof(a)"?

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 Torvalds

#36
post #7

Earlier quoted context omitted.

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

Except sizeof is a compile-time operation, not runtime. If sizeof were a function, int *foo = malloc(sizeof(*foo)); would make no sense.

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 Torvalds

#37
post #17

What kind of monsters don't use parentheses on sizeof. Is it the same guys that put { on new lines and have giant comment templates for every one line function.

Ah, to join the bikeshedding: { on newlines make imho more sense, the block of code is visually balanced that way. We have high-res screens now, no reason to pretend we're still on 80 char terminals. But work long enough in either one of those styles, and the other one will start to look strange.

We got higher resolution and with it not taller but wider screens. So following your reasoning the { should be on the right ;-)

Re: Humans should think of sizeof() as a function, says Linus Torvalds

#38
post #6

IIRC, if the argument to sizeof is a type, the parentheses are mandatory, anyway, so using it like it was a function is more consistent. I think the reason it is not a function from the standard's point of view is that C does not have any builtin functions (unless my memory totally fails me in this case), all functions have to be either defined locally or #included.

In C there are no functions that take a type as argument.

So

   sizeof(type)
is a special case anyway.

I'm all for using sizeof like a function, but that doesn't make it consistent. sizeof is just a special syntactical construct.

I like to think that sizeof is called an operator just for syntactic convenience much in the same way as typedef is a storage-class specifier.

Re: Humans should think of sizeof() as a function, says Linus Torvalds

#39

Earlier quoted context omitted.

Exactly. I heard people arguing on the Internet that you should not add parentheses when sizeof is applied to an expression, only a type. I just cannot understand why they bother. Just add a parenthesis and it is always right. Much less cognitive burden.

This is the same people that find it "better" to write JS without the semicolons.

[deleted]

Re: Humans should think of sizeof() as a function, says Linus Torvalds

#40
post #30

Earlier quoted context omitted.

I put it on the line that makes the code following it the most clear

How do you handle auto-format? If a rules doesn't survive that, its not practical, at least for me.

We don't use auto format for one because as far as I know it's not available for our languages (objective-c and swift)
Post reply on HN