On Wednesday 07 December 2005 04:09 pm, Joel E. Denny wrote: > > I had no idea the C standard made such restrictions on what computations > you perform with memory addresses. So the following is invalid C? > > #include <stdio.h> > int main() { > int x; > printf( "%p\n", (void*)(&x-1) ); > return 0; > } > > I can understand that *dereferencing* a pointer after subtraction might be > dangerous... (unless you're writing platform-specific code and, in the > case of autos, know your way around the stack). But we're not > dereferencing.
There are architectures where even loading an invalid address in a register might cause an error. It is not purely theoretical either. Imagine that that the program is running on a segmented architecture and additionally the segments are validated. BTW, 286 protected mode was exactly like that. On such an architecture decrementing an address could require segment adjustment. However since the address in the example already points at the beginning of the object, there is no guarantee that there is any valid address space before that. So, it could result in loading an invalid segment ("selector" in 286-speak, IIRC), which would cause an exception => undefined behavior. (Technically speaking, most 286 protected mode C compilers didn't use segment adjustment, so it wasn't a big problem in practice) regards, Tzvetan _______________________________________________ Help-bison@gnu.org http://lists.gnu.org/mailman/listinfo/help-bison