Messages in this thread | | | Date | 26 Dec 2003 11:02:06 -0000 | From | linux@horizon ... | Subject | Re: GCC 3.4 Heads-up |
| |
> 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. - To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/
| |