GCC 3.4 Heads-up
[email protected]
AppliedQuoted 7 lines
Similarly, what the _hell_ does the gcc extension
int a; (char)a += b;really mean? The whole extension is just braindamaged,
It means a = (int)((char)a + b).
(Modulo the fact that the value of the expression is the sum of type char and not the final value of type int.)
Applied to integer types, it *is* pretty brain damaged. But applied to pointer types, it makes a lot more sense.
This is because "a += b" in C is actually "a += b * sizeof(*a)", and sometimes you want a different *a.
In particular, 1 is a popular value.
Consider the common case of a structure which has a bunch of variable-sized blocks with a standard header:
struct foo {
unsigned type, size;
...
} a;Then you *do* have to write
a = (struct foo *)((char *)a + a->size);
and I might argue that
(char *)a += a->size;
is definitely cleaner.
Or consider the case when the structure doesn't have an explicit size and you have a big case statement for parsing it:
switch (a->type) {
case BAR:
process_bar_chunk(((struct bar *)a)++);
break;
case BAZ:
process_baz_chunk(((struct baz *)a)++);
break;
...
};Isn't that code a bit nicer looking? I put the redundant parens in to remind people that I didn't mean to write "(struct bar *)(a++)" (which also has its legitimate uses).
Necessary, no. But not "brain damaged", either.
It's well-defined and has legitimate uses.
Quoted 3 lines · On Fri, 26 Dec 2003 [email protected] wrote:
Applied to integer types, it *is* pretty brain damaged. But applied to pointer types, it makes a lot more sense.
No it doesn't.
Your example shows all the problems with the thing:
Quoted 7 lines
Or consider the case when the structure doesn't have an explicit size and you have a big case statement for parsing it:
switch (a->type) { case BAR: process_bar_chunk(((struct bar *)a)++); break;
Do you _really_ want to write unportable code for no reason?
This can trivially be done portably and readably by just adding a small bit of verbiage, ie you could have
#define next_ptr(x,type) (void *)(1+(type *)(x))
and just write
process_bar_chunk(a); a = next_ptr(a, struct bar);
or similar. Suddenly you can compile your code with any compiler you want to, including one that maybe generates better code than gcc. Including newer versions of gcc.
And suddenly people can look at your code, and understand what it is doing, even if they don't know all the gcc extensions. That's _important_.
Some extensions are fairly obvious. I think the "a ? : b" one is pretty simple, conceptually (ie you can explain it to even a novice C user without there being any confusion). But the "cast as lvalue" definitely isn't.
Quoted 1 line
It's well-defined and has legitimate uses.
It has no legitimate uses. The only upside of it is to avoid typing a few extra characters, but since using macros can make the code more readable anyway, that's not a very good argument.
Linus
Quoted 13 lines · [email protected] writes:
Or consider the case when the structure doesn't have an explicit size and you have a big case statement for parsing it:
switch (a->type) { case BAR: process_bar_chunk(((struct bar *)a)++); break; case BAZ: process_baz_chunk(((struct baz *)a)++); break; ... };
Quoted 1 line
Isn't that code a bit nicer looking? I put the redundant parens
Except that the parens are not redundant.
Quoted 2 lines
in to remind people that I didn't mean to write "(struct bar *)(a++)" (which also has its legitimate uses).
Which is why it's parsed this way by default.
Andreas.
Signature · 4 lines
Andreas Schwab, SuSE Labs, [email protected] SuSE Linux AG, Maxfeldstraße 5, 90409 Nürnberg, Germany Key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5 "And now for something completely different."