Live data from Hacker News

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

lkml.org

61–70 of 140 posts

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

#61
post #25

Earlier quoted context omitted.

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

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.

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.

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

#62

Earlier quoted context omitted.

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.

And you really shouldn't use it with a type if you can avoid it anyway, it makes code brittle e.g. int *foo; // code foo = malloc(sizeof(int)); a few months later, change foo to be a double. Code still compiles, no warning, but you're allocating half the memory you need.

[deleted]

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

#63

Earlier quoted context omitted.

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.

And you really shouldn't use it with a type if you can avoid it anyway, it makes code brittle e.g. int *foo; // code foo = malloc(sizeof(int)); a few months later, change foo to be a double. Code still compiles, no warning, but you're allocating half the memory you need.

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 Torvalds

#64

Earlier quoted context omitted.

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

I don't believe its equivalent. When I'm writing Scala code in the backend and have to switch to frontend I often find myself omitting the semi-colons but going back and fixing due to self-imposed coding conventions. For Javascript just as in Python or Scala, semi-colons are optional for a good reason.

We would all be better off if semi colons had just been required in JavaScript. The problem is that semi colons are not actually optional in JavaScript. Instead JavaScript use ASI, automatic semi colon invasion, where the compiler attempts to determine where semi colons should go. Unfortunately the rules are complex, prone to certain errors, and I can't rely on everyone I ever work with understanding all of those rules. For all of these reasons if you are writing JavaScript you should just use semi colons.

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

#65

I respect Linus greatly but the way he to talks to his fellow 'humans' is insanely confrontational, rude and disrespectful. It overshadows his arguments (which are usually very good) and throws people on the defensive. I'm grateful for all he has accomplished and shared with us but I imagine working with him is 'hell'.

Agreed. Not sure which is worse - that he's a terrible role model, or that he's proud of that fact.

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

#66

I respect Linus greatly but the way he to talks to his fellow 'humans' is insanely confrontational, rude and disrespectful. It overshadows his arguments (which are usually very good) and throws people on the defensive. I'm grateful for all he has accomplished and shared with us but I imagine working with him is 'hell'.

http://www.catb.org/esr/faqs/smart-questions.html#keepcool

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

#67
post #51

> Here's an example of a really bad use of "sizeof" that doesn't have the parenthesis around the argument: sizeof(*p)->member. Quite frankly, if you do this, you should be shot. Good ol' Linus

I certainly read that the wrong way first time around.

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

#69
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.

What advantage does 'visually balanced' give? That's like saying the letter A is better than B because it's 'stands up better.'

Indentation is how you should identify blocks.

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

#70
post #68

"Quite frankly, if you do this, you should be shot. " Ah, yes. No comment from Torvalds is complete without a gratuitous ad hominem.

To be fair, he's referring to the code `sizeof(*p)->reference`, in which case I'd probably shoot the writer too.
Post reply on HN