Live data from Hacker News

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

lkml.org

51–60 of 140 posts

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

#52

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.

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 Torvalds

#53
post #21
post #9

Earlier quoted context omitted.

Doesn't `if` always require parenthesizes?

Not even in C. You can have an if condition that is actually a macro that expands with parentheses. For example the macros in ctype.h, so you can actually write if islower(c) { (..) }

That's not portable, though: A comforming implementation may very well implement islower as a function call, and you should treat is as such.

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

#54
post #44
post #31

Earlier quoted context omitted.

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.

it looks quite disturbing without them Remove the newline and it looks ok to me: int i = 42; do printf("hey\n"); while (--i > 0); Another possibility would be: int i = 42; do printf("hey\n"); while (--i > 0);

Oohh... this would be so good for the underhanded C contest! Just mix is up with the comments which make the `do` look like (assuming good actor) an accidentally wrapped comment.

    /* The following code does something, so here's the
    // explanations of what happens. And here's what we actually
    */ do
    printf("hey\n");
    
    /* And now just count down */
    while (some_check(--i));
Now spot that in a large file of real code!

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

#55
post #35

Earlier quoted context omitted.

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 r…

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 Torvalds

#56
post #52

Earlier quoted context omitted.

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.

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));

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 Torvalds

#57
post #52

Earlier quoted context omitted.

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.

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));

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 Torvalds

#58
post #52

Earlier quoted context omitted.

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.

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));

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 Torvalds

#59
post #44

Earlier quoted context omitted.

it looks quite disturbing without them Remove the newline and it looks ok to me: int i = 42; do printf("hey\n"); while (--i > 0); Another possibility would be: int i = 42; do printf("hey\n"); while (--i > 0);

Oohh... this would be so good for the underhanded C contest! Just mix is up with the comments which make the `do` look like (assuming good actor) an accidentally wrapped comment. /* The following code does something, so here's the // explanations of what happens. And here's what we actually */ do printf("hey\n"); /* And now just count down */ while (some_check(--i)); Now spot that in a large file of real code!

Sneaky. Most syntax highlighting would make it stick out like a sore thumb, but still, sneaky.

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

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

In C99, sizeof is compile-time only if VLAs are not involved.

In C89, sizeof is always compile-time.

Post reply on HN