Re: Setting pointer to null!



On Wed, 1 Apr 2009 21:51:39 +0100, "David Webber"
<dave@xxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

The "= NULL" is technically superfluous, (if you get the code right), but
if you make a mistake afterwards and forget to assign x in one path through
the program, this makes it MUCH clearer where to look when you're debugging.

But if your code contains a subsequent comparison against NULL, you still
have a logic error.

A debug build would assign it NULL anyway

Actually, debug builds do not and AFAIK have never done that.

so why not make life eaier for
yourself and initialise it NULL on a release build. Then when it crashes
you'll know what to look for.

The best thing is to assign a non-NULL singular value. That's what debug
builds do, and I think they use a repeated 0xCD pattern. There are several
such patterns, and they are documented in MSDN somewhere. What would really
be cool is if the compiler generated extra code to disallow comparing two
such pointers, doing arithmetic on them, etc, which would make it more
foolproof. In any case, if you "defensively" initialize all pointers to
NULL, you defeat this debugging feature! For this reason, and the one
mentioned earlier, I don't believe in this sort of "defensive programming",
if that's the proper term for it.

--
Doug Harrison
Visual C++ MVP
.



Relevant Pages

  • Re: code critique wanted
    ... dew point at pressure level ... This defined the pattern @slice so that aytime it was seen later ... With pointers it's also not ... no deficiencies and the other way is to make it so complicated ...
    (comp.lang.fortran)
  • Re: Release build seg faults at vector.push_back above certain size, b
    ... >i'm using a vectors of pointers in my code. ... >the whole thing get's really strange when i use the debug build. ... set the warning level to 4 and define ... deleteptr; ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Proper tail recursion
    ... WHen fiddling with Python internals, build Python in debug mode. ... initializes newly allocated memory with another special byte pattern, ... and overwrites newly freed memory with a third special byte pattern. ...
    (comp.lang.python)
  • Re: regarding dynamic allocation for pointers
    ... that give an error when pointers are not initialised to NULL. ... There is no reason to initialize ... initialise, but to say there is *no* reason to initialise is a bit strong, ... Statically allocated variables are implicitly initialised to zero, ...
    (comp.lang.c)
  • Re: strang behaviour std:vector and XString in VC6.0
    ... > In the debug version I've the following problem: ... > After I've added a couple of pointers using the push_back function and I ... > iterate over x the program crashes in the iteration loop. ... an uninitialised variable which happens to get one value in debug mode and a ...
    (comp.lang.cpp)

Loading