Pointer Conversion and Use

Pointer Conversion and Use

Post by Steven » Thu, 21 Dec 2006 15:13:34


The C99 specification states:

*6.3.2.3(7):
"A pointer to an object or incomplete type may be converted to a
pointer to a different object or incomplete type. **If the resulting
pointer is not correctly aligned () for the pointed-to type, the
behavior is undeed. (In general, the concept €orrectly
aligned€is transitive: if a pointer to type A is correctly
aligned for a pointer to type B, which in turn is correctly aligned for
a pointer to type C, then a pointer to type A is correctly aligned for
a pointer to type C.) **Otherwise, when converted back again, the
result shall compare equal to the original pointer.** When a pointer to
an object is converted to a pointer to a character type, the result
points to the lowest addressed byte of the object. Successive
increments of the result, up to the size of the object, yield pointers
to the remaining bytes of the object.**" *

and the rationale (pg 49) on this says:

*Consequences of the treatment of pointer types in the Standard
include:
A pointer to void may be converted to a pointer to an object of any
type.
A pointer to any object of any type may be converted to a pointer
to void.
If a pointer to an object is converted to a pointer to void and
back again to the original
pointer type, the result compares equal to original pointer.
It is invalid to convert a pointer to an object of any type to a
pointer to an object of a
different type without an explicit cast.
Even with an explicit cast, it is invalid to convert a function
pointer to an object pointer
or a pointer to void, or vice versa.
It is invalid to convert a pointer to a function of one type to a
pointer to a function of a
different type without a cast.
Pointers to functions that have different parameter-type
information (including the ld-
styleabsence of parameter-type information) are different types.*


But C99 specification also states at 6.5(7):

*An object shall have its stored value accessed only by an lvalue
expression that has one of
the following types: (The intent of this list is to specify those
circumstances in which an object may or may not be aliased.)
a type compatible with the effective type of the object,
a qualid version of a type compatible with the effective type
of the object,
a type that is the signed or unsigned type corresponding to the
effective type of the
object,
a type that is the signed or unsigned type corresponding to a
qualid version of the
effective type of the object,
an aggregate or union type that includes one of the aforementioned
types among its
members (including, recursively, a member of a subaggregate or
contained union), or
a character type.*

I can not resolve these two citations, if one is permitted to typecast
one pointer type to ANY other, why is one not allowed to dereference
the new pointer? And if one is allowed to dereference the new pointer,
how does that impact on the rules of 6.5(7) which seem to limit the
allowable casts that may be dereferenced?

Further, if a type cast pointer can not be de-referenced safely what
utility does type casting of pointers (outside of the rules of 6.5(7))
provide?

Shouldn't the rule in 6.3.2.3(7) be limited by the requirements of
6.5(7)?
 
 
 

Pointer Conversion and Use

Post by kuype » Thu, 21 Dec 2006 19:43:23


...

Because there might not be a valid object of the new type at the
location pointed at by the converted value; particularly if the new
type is 'void'.


Whenever two different pointers alias each other, by pointing to
overlapping objects, if both of them can be safely dereferenced,
writing through one of the pointers requires that the next read through
the other must actually read the object it points at, rather than using
the cached result of a previous read. By declaring that certain
pointers cannot be safely dereferenced, the standard enables
implementations to ignore that issue, improving opportunities for
register optimization, at the cost of producing unexpected results if
someone ignores that restriction. The anti-aliasing rules were designed
to maximize the opportunities for such optimizations, while still
allowing all uses of aliasing pointers that the committee considered
reasonably useful.


In general, such conversions are useful as intermediate steps in a
process that will eventually produce a pointer that can be safely used
(though possibly only in a comparison, rather than a dereference).
Therefore, the answer to


is "No".