[PATCH] C undefined behavior fix
Tim Hollebeek
tim@hollebeek.com
Thu Jan 3 14:20:00 GMT 2002
> Also, what does the standard say about casting pointers to integral
> types? IIRC you aren't entitled to assume that a pointer will fit in
> any integral type, or anything about the bit patterns that you get.
Yes, but you're on much safer ground here, since the conversion is
implementation defined. Practical considerations almost guarantee the
implementation choice will be "int == address" for targets where this
makes sense (e.g. has a simple, flat, contiguous address space).
Also, in the latest version of the C standard, you do have a int type
that can contain a pointer.
Allowing people to treat pointers as if they were "just" integers
prevents a whole slew of interesting and useful compiler
transformations, which is why the standard frowns upon such behavior.
Buffer overflow checks are an example. It's possible to build bounded
pointer implementations for strict ANSI C, but impossible for the "all
pointers are just integers" variant.
Do the compiler a favor. If you're playing with pointers as if they
are integers, make them integers. Types are your friend.
-Tim
More information about the Gcc
mailing list