In article <11**********************@g47g2000cwa.googlegroups .com>,
<fo*@scene.pl> wrote:
I'm disappointed with your answers.
Here's a program that differentiates case 'b' from 'a' and 'c':
signal(SIGBUS, bus_err);
You asked for a portable standard program.
http://www.opengroup.org/onlinepubs/.../signal.h.html
"The ISO C standard only requires the signal names SIGABRT, SIGFPE,
SIGILL, SIGINT, SIGSEGV, and SIGTERM to be defined."
You do check #ifndef SIGBUS but you return a constant value
if it is not defined, which is NOT a portable way of detecting
whether unaligned accesses are permitted or not.
Your original posting also asked for three cases to be differentiated
and said that the second (no support) and third (software support)
need not be differentiated, but your program cannot differentiate
the third case from the first, and so fails the specifications that you
yourself posted.
You are also mis-using SIGBUS according to opengroup:
SIGBUS Access to an undefined portion of a memory object.
but in your program, the portion was defined, but was being (potentially)
misaddressed. That case is closer to
SIGSEGV Invalid memory reference.
If you found experimentally that you needed to use SIGBUS on
a system instead of SIGSEGV, then clearly on that system there
are system dependancies that make the usage non-portable (SIGSEGV
being the ISO C signal that would appear to be appropriate.)
ISO C says, with respect to the aborting signals,
An implementation need not generate any of these signals, except as
a result of explicit calls to the raise function.
The standard also indicates,
If and when the function returns, if the value of sig is SIGFPE,
SIGILL, SIGSEGV, or any other implementation-defined value
corresponding to a computational exception, the behaviour is
undefined; otherwise the program will resume execution at the
point it was interrrupted.
I would need to look somewhat further to see whether the state
that is restored by longjmp() includes clearing the undefined
state implied by the above paragraph.
--
If you lie to the compiler, it will get its revenge. -- Eric Sosman