In article <6a**********************************@u10g2000prn. googlegroups.com>,
TDB <vn*****@gmail.comwrote:
>int main() {
void *p;
p=(void *)0x00000005;
// Here
printf(" %d ",* (int *)p);
return 0;
}
When I executed the above code using GCC and VC++, it caused
segmentation error and windows error..
I need a condition that can find whether the pointer address will
cause error or not..
Any suggestions to construct that condition ?
There is no facility in standard C that would allow you to make
that determination.
For any given pointer value, the answer of whether an access
would lead to an error or not can be dynamic, changing over time.
For example the pointer might be to an object that used to exist
but which has been returned to the operating system with the
address having been removed from range of accessible virtual addresses.
Alternately, the pointer might happen to be to a location that
was not in range before, but is in range right now.
Or the pointer might happen to be to an I/O address to a device
that is wandering in and out of availability (e.g., USB devices
get unplugged, SCSI addresses have bus faults.)
You may wish to investigate use of signal() and SIGSEGV .
It is not certain that a SIGSEGV will be raised for every illegal
access (indeed, it is unlikely on Unix machines, which tend to
have a finer discrimination for different kinds of illegal access),
but you might happen to find that on every implementation you wish
to port to that it is sufficient for whatever it is you are trying to do.
--
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth