Re: Clarification on type punning.



Francis Moreau wrote:
On Aug 24, 5:08 pm, jameskuyper <jameskuy...@xxxxxxxxxxx> wrote:

Type punning is both more AND less that what's done by a cast
operator.

It's more than what's done by a cast operator, because it's not
sufficient to convert a pointer of one type into a pointer of a
different type; it order for type punning to occur, the converted
pointer must then be dereferenced, which necessarily involves at least
one more operator.

ok so that means that type punning happens only with pointer cast.

No. It indicates that using a pointer cast is part (but not the whole)
of one way of performing type punning. Other ways include the use of
unions and reliance on implicit conversions through void*, as I
indicated in the part of my message that you skipped,

Type punning is also less than what's done by the cast operator,
because most uses of the cast operator don't involve pointers at all,
and even the ones that do aren't necessarily part of a type-pun.

Really ?

I saw ton of code out there which uses pointer casts was used to
implement inheritance in C.

I'm sure it depends upon the body of code you're inspecting. Type
punning is almost always unportable, and (with certain exceptions)
we're not allowed to write code which depends upon unportable
assumptions. Most of the casts that occur in our code come from a
customer mandate that our code must not rely upon implicit
conversions, but I won't count those, because I think that's a
seriously wrong-headed mandate. The remaining casts serve mostly to
make conversions happen that need to occur and which wouldn't occur
implicitly, most commonly as a result of using a variadic function
like members of the printf() or scanf() family.

If I needed to do a lot of inheritance, I'd favor using a language
where it's a built-in feature, rather than wasting time emulating in a
language where it isn't.
However, if you decide that you must emulate inheritance in C, it can
be done without the undefined behavior that is typical of most type-
punning: just define a union type whose members are of the different
struct types, and use the union type to perform the type punning,
where the "common initial sequence" that is referred to in 6.5.2.3p5
is the part that is inherited.

.



Relevant Pages

  • Re: Clarification on type punning.
    ... It indicates that using a pointer cast is part ... of one way of performing type punning. ...
    (comp.lang.c)
  • Re: Clarification on type punning.
    ... Type punning is both more AND less that what's done by a cast ... Type punning is also less than what's done by the cast operator, ... implement inheritance in C. ...
    (comp.lang.c)
  • Re: void* question
    ... With a cast operator, like `ptr' or `ptr'. ... More often than not you should not cast the pointer at all. ... pointer to void is implicitly converted by assignment ...
    (comp.lang.c)
  • Re: void* question
    ... With a cast operator, like `ptr' or `ptr'. ... More often than not you should not cast the pointer at all. ... pointer to void is implicitly converted by assignment ...
    (comp.lang.c)
  • Re: Clarification on type punning.
    ... I did some googling to understand what "type punning" means and I ... It's more than what's done by a cast operator, ... sufficient to convert a pointer of one type into a pointer of a ... It can also be done using implicit type conversions ...
    (comp.lang.c)