Re: Array of pointers. How to check index without failure?
On Aug 3, 5:38 am, Jeff <jeff.griff...@gmail.com> wrote:
On Aug 2, 8:41 pm, I V <ivle...@gmail.com> wrote:
On Thu, 02 Aug 2007 17:21:42 -0700, Vaibhav Nivargi wrote:
Daniel T. wrote:
Set the values to 0 or NULL. Check to see if the value is 0 before
To protect against dangling pointers, a better approach
would be to use an extra 'initialized' bit for each
pointer set to 1/0 appropriately.
How would that help? You'ld have to manually set/clear the
initialized bit whenever the status changed, and I would
have thought that's no easier or harder than setting the
pointer to 0.
Be careful using the "if(p)" syntax; it does an integer
comparison on the pointer which isn't valid on all platforms.
No it doesn't. The condition in an if must have type bool.
There is an implicit conversion of pointer to bool, which is
basically the equivalent of "p != NULL". Code like "if (p)" may
be unreadable, and it's not the sort of thing you'd ever see in
well written C++, but it is perfectly legal and well defined.
Most compilers have an option to prefill any static memory
block to zeros;
I've never heard of a compiler with such an *option*. The
language standard requires that all objects with static storage
duration be "zero initialize" before the program starts. "Zero
initialized" means, basically, that each low level type is
initialized as if 0 were assigned to it. Again, there is a
special conversion, so that the integer value 0 converts to a
null pointer; even if null pointers aren't all bits 0, a pointer
with static storage duration is guaranteed to be initialized
with a null pointer value. (The one exception is in a union.
Only the first element of a union is zero initialized.)
use it if it is available and then
"if( ((X*)p) != ((X*)0))". It's a little harder for you to write, but
guarantees a valid comparision.
You don't have to get carried away:
if ( p == NULL )
if ( p == 0 )
work perfectly well, and are perfectly readable.
One other option that IBM used in it's version of the Xerces
library was to surround all object references with a
"try-catch" block and then they surpress the exception when
they hit an invalid pointer. This works, but it's a real
performance hog. An XML parser that I had to maintain (without
being allowed to change it) spent nearly 50% of its runtime in
these exception handlers.
I doubt that it was written in C++. In C++, dereferencing a
null pointer results in undefined behavior; on typical desktop
machines, it will cause a fatal program crash (core dump, in
Of course, since the behavior is undefined, an implementation is
free to do whatever it wants, including raise an exception.
IMHO, that would not be very good from a QoI point of view, but
it would certainly be conforming (as would be formatting your
James Kanze (GABI Software) email:email@example.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34