How to validate a pointer

H

Hilary Zhang

Hi,

I have a headache with my program, which is a real-time graphic program
written in VC++ 6.0. I often get an error of "access violation" when run in
release mode. But if I use debug mode, it seldom happens since the debug
mode is much slower than the release mode and the possible bug won't show
up. I think it may be problem with my usage of pointers. I just wonder is
there a way to validate a pointer, when it is 0xcdcdcd or 0xffffffff instead
of NULL(0x000000).

Thanks in advance.
Hilary
 
J

John Harrison

Hilary Zhang said:
Hi,

I have a headache with my program, which is a real-time graphic program
written in VC++ 6.0. I often get an error of "access violation" when run in
release mode. But if I use debug mode, it seldom happens since the debug
mode is much slower than the release mode and the possible bug won't show
up. I think it may be problem with my usage of pointers. I just wonder is
there a way to validate a pointer, when it is 0xcdcdcd or 0xffffffff instead
of NULL(0x000000).

Thanks in advance.
Hilary

There is no portable way to validate a pointer. This group only discusses
portable (i.e. standard) C++. You need to ask on a Windows programming
group. I think Windows has a function called IsValidPtr or something, but
ask on a Windows group, not here.

john
 
D

Deming He

Hilary Zhang said:
Hi,

I have a headache with my program, which is a real-time graphic program
written in VC++ 6.0. I often get an error of "access violation" when run in
release mode. But if I use debug mode, it seldom happens since the debug
mode is much slower than the release mode and the possible bug won't show
up. I think it may be problem with my usage of pointers. I just wonder is
there a way to validate a pointer, when it is 0xcdcdcd or 0xffffffff instead
of NULL(0x000000).

Thanks in advance.
Hilary

Initialize a pointer to NULL and assign it also NULL when it supposes not
pointing to somewhere. Then before using, check it against NULL.
(IsBadCodePtr, IsBadStringPtr, IsBadWritePtr and IsBadStringPtr in MS
Platform SDK may help you as well.)
 
P

Phlip

Hilary said:
I have a headache with my program, which is a real-time graphic program
written in VC++ 6.0. I often get an error of "access violation" when run in
release mode. But if I use debug mode, it seldom happens since the debug
mode is much slower than the release mode and the possible bug won't show
up. I think it may be problem with my usage of pointers. I just wonder is
there a way to validate a pointer, when it is 0xcdcdcd or 0xffffffff instead
of NULL(0x000000).

Write lots of unit tests to make surprise differences between debug and
release mode less likely. You can call the offending code more often, in
more ways, and not worry about performance differences in debug mode.

NULL all your pointers before use. Don't rely on platforms that write 0s or
0xDEADBEEF into unused memory before you allocate it. C++ only defines the
act of sampling the value of a pointer that actually points to a real object
of its type, or one-off-the-end of an array of said objects, or that
contains NULL. Your compiler probably does not mind, but the behavior of
checking if a pointer contains 0xffffffff, unless something of the correct
type happens to be up there, is undefined.

Put all pointers into very small objects - call them "handles" - whose only
job is to manage their pointers. Research "Resource Acquisition is
Initialization", and "smart pointers" to learn how.

Write lots of unit tests to make certain these handles at least present the
appearance that they work.

Divide your program in half, and try to reproduce the bug in each half. Take
the half that has the bug, divide it into quarters, check for the bug, and
keep going until you have only a small region to search.

Next time you start a program, don't wait so long before stress-testing.
Write lots of unit tests as you go, and stress-test these.

Oh, one more very important tip: Write lots of unit tests.
 
R

Ron Natalie

John Harrison said:
There is no portable way to validate a pointer. This group only discusses
portable (i.e. standard) C++. You need to ask on a Windows programming
group. I think Windows has a function called IsValidPtr or something, but
ask on a Windows group, not here.

The bit patterns he is commenting on are put in there by the debug version for
otherwise uninitialized and memory returned by free.

IsValidxxxPtr functions don't tell you if the pointer is valid, just whether you would
trap if you were to do a read/write access with it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,160
Messages
2,570,890
Members
47,423
Latest member
henerygril

Latest Threads

Top