473,387 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Finding invalid pointers

TDB


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 ?
Feb 4 '08 #1
8 1249
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

Feb 4 '08 #2
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
Feb 4 '08 #3
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
Feb 4 '08 #4
On 4 Feb, 18:57, TDB <vnrb...@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 ?
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.
Feb 4 '08 #5
In article <6a**********************************@u10g2000prn. googlegroups.com>,
M.Caggiano <Mi**************@gmail.comwrote:
>On 4 Feb, 18:57, TDB <vnrb...@gmail.comwrote:
>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
Feb 4 '08 #6
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

Feb 4 '08 #7
CBFalconer wrote:
>
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 :-)
%@#&! So can I. :-b

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto
Feb 5 '08 #8
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..
Why? If you need a hard-coded address (as opposed to the address of an
object you have or allocate), what you can find there is extremely
machine-dependent.

--
Army1987 (Replace "NOSPAM" with "email")
Feb 5 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: FKothe | last post by:
Hello together, the program below shows a behavior i do not understand. When compiled with the HX-UX11 c-comiler ( version B.11.11.04 ) v2.p in function test_it0 points to an invalid adress and...
15
by: pemo | last post by:
Are there standard macros that a (c99) compliant compiler should support? I note that in the c99 std that __FILE__ __LINE__ __func__ are mentioned, but others (that seem to be built in to...
0
by: Ben Holness | last post by:
Hi all, I have a system which allows users to enter a message on a (PHP) website. This message is then put into a (MySQL) Database. A perl script then picks up the message and creates an XML...
2
by: Bardo | last post by:
Hi all, I am a newbie to using the XML Schema Object Model (SOM) and would like a few pointers on how to perform a particular task. I am essentially trying to search for elements/attributes via...
6
by: Martin | last post by:
Hi I need to maintain a <setof pointers to objects, and it must be sorted based on the values of pointed objects, not pointer values. I can achieve this easily by defining my own comparing...
3
by: garyusenet | last post by:
Some time ago I enquired about how I interface with a program written in an old version of C++ Any terms i use like list that follow are used in their common everyday usuage! One of the...
7
by: Jason Heyes | last post by:
Why can't I use the find algorithm to find a program argument in the following way? std::find(argv, argv + argc, "-v") Thanks.
2
by: Sebastian Schucht | last post by:
Hi, I have a templateclass with virtual member-functions, so i can't create instances ... but for returning the result i would like use one. So i replaced the A<TTypefrom the baseclass with the...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.