[comp.std.c] increment casted void pointer -- ANSI?

bhoughto@pima.intel.com (Blair P. Houghton) (03/27/91)

In article <15589@smoke.brl.mil> gwyn@smoke.brl.mil (Doug Gwyn) writes:
>In article <288@nazgul.UUCP> bright@nazgul.UUCP (Walter Bright) writes:
>-In article <1991Mar8.165300.11692@zoo.toronto.edu> henry@zoo.toronto.edu (Henry Spencer) writes:
>-/There is no way to say "pretend this variable has a different type" in C.
>-The usual to do a 'type paint', which is to change the type of an object
>
>A strictly conforming method is to use a union type.

Ouch!  My ears just pricked up so hard I think I pulled my skull...

Where in the std is it prohibited to fake unions by using a struct
and externally prohibiting attempts to dereference a value
that was superseded by one of a different type?

It seems to say only that a union has to be at least large enough
to hold it's member with the most bits, and to align members
suitably, but not that it has to overlap types.

The rationale parrots the "a pointer to a union, suitably
cast, points to each member".  Considering the latitude
given a cast, this may mean that the pointer could be
adjusted in byte-increments to point to the corresponding
type's member.  This implies strongly that overlap
must occur if there is more than one member with the
same type, but that doesn't buy too much "bit-paint".

I only saw the one paragraph, though (ANSI X3.159-1989,
sec. 3.5.2.1, p. 62, ll. 10-14), so I'd be glad if someone
came up with more stringent, explicit delineation of this
situation.

				--Blair
				  "Time to make the donut holes..."

gwyn@smoke.brl.mil (Doug Gwyn) (03/29/91)

In article <3492@inews.intel.com> bhoughto@pima.intel.com (Blair P. Houghton) writes:
-In article <15589@smoke.brl.mil> gwyn@smoke.brl.mil (Doug Gwyn) writes:
->In article <288@nazgul.UUCP> bright@nazgul.UUCP (Walter Bright) writes:
->-In article <1991Mar8.165300.11692@zoo.toronto.edu> henry@zoo.toronto.edu (Henry Spencer) writes:
->-/There is no way to say "pretend this variable has a different type" in C.
->-The usual to do a 'type paint', which is to change the type of an object
->A strictly conforming method is to use a union type.
-Where in the std is it prohibited to fake unions by using a struct
-and externally prohibiting attempts to dereference a value
-that was superseded by one of a different type?

There are constraints on what is strictly conforming use of pointed-to
objects, etc.

-It seems to say only that a union has to be at least large enough
-to hold it's member with the most bits, and to align members
-suitably, but not that it has to overlap types.

Sure it "overlaps", or acts as if it did.  A conforming implementation
that, e.g. stores floating-point data in a different address space from
integer data may have to jump through hoops if it wishes to make the
aliasing of the representations actually work.  However, such a program
would have to be successfully translated, it just may not work right (in
which case the fake pointer business would probably malfunction too).