Re: [ROOT] zero pointers on NT

From: Anton Fokin (anton.fokin@smartquant.com)
Date: Thu Jan 25 2001 - 13:33:10 MET


Ok,
By some reason I though pointers initialized to 0 by default. By the way,
why is this not a standard? It would be quite convinient ..., at least for
me :)

/Anton

Pointers
Never let pointers uninitialized. If they are not pointing to anything
initialize them with NULL (or 0, depending on compiler).
Usually the standard functions which return pointers return NULL (or 0) in
case of error.

For the case of the return value==NULL, ways of the recovery of the program
must exist.
For the case of the return value!=NULL, we can use the returned value.
After every memory deallocation, the pointer must be initialized with
NULL(or 0).
Every time you use a pointer you must verify that it is different from NULL.

For the case of the return value==NULL, ways of the recovery of the program
must exist.
For the case of the return value!=NULL, we can use the returned value.
Every time an error is detected in the debug version, it must be made public
by "printf(...)" , "TRACE(...)" ....
Every function you create which returns a pointer must return NULL (or 0) in
case of error.
Always deallocate the memory you have allocated.
Example

#define MY_TABLE_LENGTH    100

class MyClass
{ 
    long *m_pltable; 
    MyClass(); 
    ~MyClass(); 
}; 

MyClass::MyClass() 
{ 
    m_pltable=NULL;                                                 // initialize the pointer by NULL, later we will know it is free 
}; 
  
MyClass::MyFunction() 
{ 
    if (m_pltable==NULL)                                            // check if the pointer is free, if not it is however pointing to a memory zone 
                                                                                  // if it is different by NULL and we would allocate memory again for it, 
                                                                                  // the old memory zone would be lost 
        m_pltable=(long *)malloc(MY_TABLE_LENGTH*sizeof(long)); 

    if (m_pltable!=NULL)                                        
    // check if the pointer points to memory zone


        for(int i=0;i<MY_TABLE_LENGTH;i++)
           m_pltable[i]=0;                                              //
use the pointer
    }
}

MyClass::~MyClass()
{ 
   if (m_pltable==NULL)                                           // check if the pointer is pointing to a memory zone 
       free(m_pltable) ;                                                 // free the memory zone 
}; 


----- Original Message ----- 
From: Fons Rademakers <Fons.Rademakers@cern.ch>
To: Anton Fokin <anton.fokin@smartquant.com>
Cc: roottalk <roottalk@pcroot.cern.ch>
Sent: Thursday, January 25, 2001 12:23 PM
Subject: Re: [ROOT] zero pointers on NT


> IIRC only globals and class static variables are initialized to 0.
> All others need to be initialized.
> 
> -- Fons
> 
> 
> Anton Fokin wrote:
> > 
> > Hi rooters,
> > 
> > I am a bit confused with pointer initialization on win32 and VC6.0.
> > 
> > C++ standard says it guarantees that you get zero pointer when you define a
> > pointer. It doesn't seem to be true uder VC. Any commments?
> > 
> > Regards,
> > Anton
> 
> -- 
> Org:    CERN, European Laboratory for Particle Physics.
> Mail:   1211 Geneve 23, Switzerland
> E-Mail: Fons.Rademakers@cern.ch              Phone: +41 22 7679248
> WWW:    http://root.cern.ch/~rdm/            Fax:   +41 22 7677910
> 
> 



This archive was generated by hypermail 2b29 : Tue Jan 01 2002 - 17:50:34 MET