News for hackers. Stuff that matters.
Re:Humans should think of sizeof() as a function, says Linus... by weinzierl on Saturday May 30, 2015 @08:18AM (#9629915)
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... by masklinn on Saturday May 30, 2015 @08:36AM (#9629963)
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.
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.

Re:Humans should think of sizeof() as a function, says Linus... by imglorp on Saturday May 30, 2015 @08:54AM (#9630008)
Which is why it's nice to lift stuff out into typedefs. It centralizes them (DRY principle) and avoids this issue.

    typedef int thing_t;
    ...
    thing_t *foo;
    // code
    foo = malloc(sizeof(thing_t));
Re:Humans should think of sizeof() as a function, says Linus... by viraptor on Saturday May 30, 2015 @09:10AM (#9630055)
Why is that better than just getting `sizeof(* foo)`? Even with typedef, I can see this happening in the future:

    typedef int thing_t;
    ...
    thing_t *foo_internal;
    thing_wrapper_t *foo;
    // code
    foo = malloc(sizeof(thing_t));
If you do:

    foo = malloc(sizeof(*foo));
That's at least always on the same line.
Re:Humans should think of sizeof() as a function, says Linus... by userbinator on Saturday May 30, 2015 @09:11AM (#9630057)
That just makes it more verbose.

This, on the other hand, always allocates one object of foo's pointed-to-size, whatever its type:

    foo = malloc(sizeof(*foo));
Re:Humans should think of sizeof() as a function, says Linus... by dllthomas on Saturday May 30, 2015 @03:04PM (#9631367)
As an aside, I think nearly any time you want a typedef, it's worth wrapping it in a struct.

    typedef struct { int value; } thing_t;
That way the compiler catches it when you try to pass the wrong thing (at least, more of the time).
Re:Humans should think of sizeof() as a function, says Linus... by dllthomas on Saturday May 30, 2015 @03:08PM (#9631384)
DRY is good, but making the structure of your code reflect the actual semantics you want is better. What you want is to allocate space for foo. So write that.
Re:Humans should think of sizeof() as a function, says Linus... by crazy2be on Saturday May 30, 2015 @09:07AM (#9630050)
Well, in this (simple) case you can just do

    int *foo;
    foo = malloc(sizeof(*foo));
And avoid the brittleness mentioned.
Re:Humans should think of sizeof() as a function, says Linus... by ksherlock on Saturday May 30, 2015 @10:25AM (#9630245)
There are standard macros (va_arg, I'm looking at you) that take a type as an argument.