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.
[ Parent
| on HN ]
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).