Good ol' Linus
Humans should think of sizeof() as a function, says Linus Torvalds
51–60 of 140 posts
Re: Humans should think of sizeof() as a function, says Linus Torvalds
#52Earlier 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.
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
#53Earlier 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) { (..) }
Re: Humans should think of sizeof() as a function, says Linus Torvalds
#54Earlier 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);
/* 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
#55Earlier 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…
Re: Humans should think of sizeof() as a function, says Linus Torvalds
#56Earlier 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));
int *foo;
foo = malloc(sizeof(*foo));
And avoid the brittleness mentioned.Re: Humans should think of sizeof() as a function, says Linus Torvalds
#57Earlier 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));
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
#58Earlier 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));
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
#59Earlier 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!
Re: Humans should think of sizeof() as a function, says Linus Torvalds
#60Earlier 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 C89, sizeof is always compile-time.