Re: "C vs java"



stremler@xxxxxxxxxxxxxx writes:
begin quoting Richard Heathfield <rjh@xxxxxxxxxxxxxxx> :
stremler@xxxxxxxxxxxxxx said:

begin quoting Richard Heathfield <rjh@xxxxxxxxxxxxxxx> :

<snip>

The "casting" row says "anything goes", which is simply wrong.

I wish to hear more on this; what can't you cast in C?

Well, for one thing, you can't cast structs or unions. (C99 muddies the
waters a little with compound literals, but the document was certainly
addressing C90, so I'll confine my answer to that.)

/me tests

Hm... indeed. You have to abuse void * instead of casting to get that
sort of thing to happen.

But it's not quite the same thing. A cast applied to a struct, if it
were legal, might plausibly convert between two sufficiently similar
struct types by copying member values.

But my rejection of
"anything goes" was really a rebuttal of the very popular idea that
casting is some kind of magic wand that can turn anything into anything
else. It isn't. A cast converts a value from one type to another type, but
makes no guarantees that the conversion makes sense. For example, you can
cast a long int into a pointer to struct tm if you want, but that doesn't
mean you'll get anything useful out of it. In fact, almost all casting in
C is wrong.

I've always thought of a cast as an instruction to the compiler to trust
the programmer about types in this instance; a sort of "No, really, I know
what I'm doing, I want to treat this object as THAT type, truly, trust me."

If actual conversion of data (as opposed to coercion of type) is involved,
yah, it's probably wrong.

A cast performs a value conversion; it doesn't (necessarily) treat an
object as being of a specified type. For example, ``(double)42''
yields the value 42.0 of type double.

Pointer conversions *typically* work by reinterpreting the
representation as the specified type, but that's not how the operation
is defined; on an implementation where different pointers have
different representations, an actual conversion will have to be
performed. And converting a pointer to an integer *of a different
size* will have to trim or pad bits, and might have to do more than
that.

Pointer conversion can be used to implement type-punning of the
pointed-to object, but the conversion itself doesn't do type-punning.

The point is that most of the cases where a conversion is portable can
be expressed as an implicit conversion rather than a cast. Taking
advantage of an implicit conversion lets the compiler figure out the
relevant types, rather than requiring the programmer to do so (and to
get it right with no help from the compiler).

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.