|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
TDB wrote:
> > 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 ? Peek/poke games require either having access to a memory map, hooks into the OS (for dynamically-allocated areas), or some way to intercept the segmentation error and render it non-fatal. If you don't have one of those three facilities, then you're stuck with the existing mechanism that detects the invalid access and terminates the program. FYI *p == 0xC3 on all CP/M systems. :-) -- Morris Dovey DeSoto Solar DeSoto, Iowa USA http://www.iedu.com/DeSoto |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 ? |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
TDB wrote:
> void *p; > > p=(void *)0x00000005; > > // Here > printf(" %d ",* (int *)p); [..] > 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.. There isn't any that is not system-dependant. As a rule of thumb, make sure that pointers are zero unless they point to something real. Then, you can simply check them with 'if(p)' or 'if(p!=NULL)'. Uli |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
In article <6afed105-35a0-43d3-90a0-d1b703b01ba9@u10g2000prn.googlegroups.com>,
TDB <vnrbabu@gmail.com> wrote: >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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
On 4 Feb, 18:57, TDB <vnrb...@gmail.com> wrote:
> 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 ? In my opinion, you are wrong to use the pointer in that way. If you've declared p with void, but after you've used then it as an int. |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
In article <6acdf699-c9f4-42b7-be61-e9d5c73323c0@u10g2000prn.googlegroups.com>,
M.Caggiano <Michele.Caggiano@gmail.com> wrote: >On 4 Feb, 18:57, TDB <vnrb...@gmail.com> wrote: >> int main() { >> void *p; >> p=(void *)0x00000005; >> // Here >> printf(" %d ",* (int *)p); >> return 0; >> } >In my opinion, you are wrong to use the pointer in that way. If you've >declared p with void, but after you've used then it as an int. It would not have mattered for the purpose of his question if his code had been int main() { int *p; p = (int *)0x00000005; printf(" %d ",* (int *)p); return 0; } This code is not inherently invalid: C implementations are permitted to define meaningful conversions between integral values and pointers, but they are not required to, and the result of the dereference is of course undefined if the resulting pointer is not properly aligned or does not point to an object. -- "I will speculate that [...] applications [...] could actually see a performance boost for most users by going dual-core [...] because it is running the adware and spyware that [...] are otherwise slowing down the single CPU that user has today" -- Herb Sutter |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
Morris Dovey wrote:
> TDB wrote: > >> 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 ? > > Peek/poke games require either having access to a memory map, > hooks into the OS (for dynamically-allocated areas), or some way > to intercept the segmentation error and render it non-fatal. > > If you don't have one of those three facilities, then you're > stuck with the existing mechanism that detects the invalid > access and terminates the program. > > FYI *p == 0xC3 on all CP/M systems. :-) I can make it work with 0xCD :-) -- [mail]: Chuck F (cbfalconer at maineline dot net) [page]: <http://cbfalconer.home.att.net> Try the download section. -- Posted via a free Usenet account from http://www.teranews.com |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
"M.Caggiano" <Michele.Caggiano@gmail.com> writes:
> On 4 Feb, 18:57, TDB <vnrb...@gmail.com> wrote: >> 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 ? > > In my opinion, you are wrong to use the pointer in that way. If you've > declared p with void, but after you've used then it as an int. No, that's not what he's doing. The assignment converts the value 0x00000005 from int to void*. The result of the conversion is implementation-defined, but it's legal. In the printf call, the value of p is converted from void* to int* by the cast, and then the resulting int* value is dereferenced, yielding a value of type int. Perhaps you missed the "*" operator (as I briefly did on my first reading). The spacing (no blank after the comma, blank after the "*") makes that an easy mistake to make. -- Keith Thompson (The_Other_Keith) <kst-u@mib.org> Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" |
|
![]() |
| Outils de la discussion | |
|
|