G
Gordon Burditt
Not necessarily if you use the results afterward *as floats* or *as
But since C does not guarantee that this same code will work on a
PDP-11, it's not safe. "undefined behavior" doesn't guarantee that
it won't do exactly what you expect it to do (until, of course, the
boss is watching).
If it's a matter of the implementation itself, *IT'S NOT SAFE*.
Setting the pointer *AND THEN USING (but not DEREFERENCING) IT*
isn't safe. The following can possibly abort the program:
char *p = 0xdeadbeef; /* or some other oddball hex constant */
p; /* note that I didn't say *p here */
Note, for example, that in protected mode putting certain bit
patterns into a segment register on an i386 platform cause traps
without even trying to dereference anything.
Anything that is not guaranteed to be safe from the C point of view
is unsafe from the C point of view.
Gordon L. Burditt
Well, yes and no. I mean, when I was using DOS (more than a decade
ago), an important pointer was the one at the interrupt vector table,
or the video memory (0x0000 and 0xA000 if I remember), and it was
always done by setting a ptr to a number. Resetting the IVT by
memsetting, was dangerous, but if you wanted to do some tricks, it was
a possible choice.
But since C does not guarantee that this same code will work on a
PDP-11, it's not safe. "undefined behavior" doesn't guarantee that
it won't do exactly what you expect it to do (until, of course, the
boss is watching).
The meaning of what it contains, should be of no concern for C, as it's
a matter of the implementation itself.
If it's a matter of the implementation itself, *IT'S NOT SAFE*.
Whether you're accessing a non-readable memory by deferencing a weird
ptr, it's again the implementation, but setting the ptr still results
in a safe operation. Writing and even reading to that location is,
naturally, not safe.
Setting the pointer *AND THEN USING (but not DEREFERENCING) IT*
isn't safe. The following can possibly abort the program:
char *p = 0xdeadbeef; /* or some other oddball hex constant */
p; /* note that I didn't say *p here */
Note, for example, that in protected mode putting certain bit
patterns into a segment register on an i386 platform cause traps
without even trying to dereference anything.
For floats, again, NaN may be a result of a misinterpretation of what
the float was meant to be, but it cannot be considered unsafe from C
point of view.
Anything that is not guaranteed to be safe from the C point of view
is unsafe from the C point of view.
It's the programmer who must be sure of all the meanings
Gordon L. Burditt