Re: variable scope problem in CINT ?

From: Fons Rademakers (Fons.Rademakers@cern.ch)
Date: Tue May 27 1997 - 12:40:29 MEST


Hoi Nick,

   the problem is a misunderstanding. Let me explain. An object
created via new is referenced via a pointer (32 or 64 bit entity
containing the memory address to the object), like:

void stat()
{
   Sample *s2 = new Sample;  // s2 is pointer to allocated Sample object
                             // this Sample object will exists till it
                             // get explicitely deleted by delete
   ..
}                            // however, the pointer s2 containing the
                             // address of the Sample object goes out
                             // of scope at this point. What happens
                             // here is that you lose the last reference
                             // to object Sample. There is no way to
                             // retrieve the address to Sample and
                             // therefore the object is lost on the
                             // heap. I.e. this is a memory leak since
                             // you have no way anymore to call delete

Don't forget, a pointer is like any other basic type. At the end
of the scope it disappears. So s2 disappears and with it the address
of Sample. To prevent this from happening you have to store the
pointer to Sample somewhere. E.g. in a global pointer or a container
(like TList) or pass the pointer as argument to a function, like:

void fun1(Sample *s)
{
   s->print(1,2);
   delete s;
}

Concerning ::s2 this is the notation to denote global scope (see
Stroustrup 2nd ed. page 45/46). Like TMyClass::s2 denotes a static
class variable s2 defined in class TMyClass. In this way one can 
differentiate between variables with the same name defined in 
different scopes. If there exist only one s2 in global scope it 
is not necessary to prepend the ::, only in case of a name clash. 
Also this is another reason to adhere to the ROOT coding conventions
(http://root.cern.ch/root/Conventions.html). Using these conventions
you would always give a global variable the name gS2 and a static 
class variable the name fgS2 and a local variable the name s2. In 
this way it is immediately clear what kind of variable you are 
dealing with.

Cheers, Fons.


Nick van Eijndhoven wrote:
> 
> *** Fons Rademakers wrote :
> >
> > Hi Nick,
> >
> >    scope is delimited by { } pairs. So for example:
> >
> > void fun1()
> > {
> >  Sample* s2;
> >  cout << " fun1 print for Sample s2 " << endl;
> >  s2->print(1,2);
> > }
> >
> > here s2 is not initialized (and set to 0 in CINT, and set to random memory
> > address in g++). s2 is defined in the fun1 scope and does not get the
> > s2 value defined in stat(). Therefore the CINT behaviour is correct.
> > You can place variables in "global" scope by putting them outside any {}.
> > For example:
> >
> > Sample *s2;
> >
> > void stat()
> > {
> >  ...
> >  s2 = new Sample;
> > }
> >
> > fun1()
> > {
> >    ...
> >    s2->print(1,2);
> >    ...
> > }
> >
> > Note that when you do:
> >
> > fun1()
> > {
> >    Sample *s2;
> >    ...
> >    ::s2->print(1,2);  // works, accesses global s2 initialized in stat()
> >    s2->print(1,2);    // accesses local uninitialized s2. Crash!
> > }
> >
> > Cheers, Fons.
> >
> Hi Fons,
> Dit begrijp ik niet, daar mijn C++ boekje zegt dat inderdaad
> 
> Sample s2;
> 
> de s2 alleen binnen de block scopes (i.e. binnen {..} ) bestaat, maar
> wanneer ik een pointer gebruik (i.e. Sample* s2) de scope van s2 wordt
> verruimt tot het moment dat ik hemzelf expliciet delete (i.e. delete s2;).
> Dit klopt ook met het verhaal wat Rene mij vertelde i.v.m. een uitleg
> van de speciale ROOT scope (welke global is) en verder is de g++
> functionaliteit ook in overeenstemming met mijn opvatting.
> Als je wil kan ik je de titel + pag. nummers van mijn boek geven; het
> kan natuurlijk zijn dat ik het zaakje verkeerd begrijp, maar zowel Rene's
> verhaal als de g++ ervaring wijzen daar niet op (ik zal je Rene's verhaal
> in een separate mail toesturen).
> Als ik het toch bij het rechte eind blijk te hebben is er dus een verschil
> tussen CINT en 'standaard' C++ en zou CINT wellicht moeten worden aangepast.
> Verder is de ::s2->print() truc nieuw voor mij, is dit een standaard manier ?
> 
>                                                           Groeten,
>                                                            Nick.
> 


-- 
Org:    CERN, European Laboratory for Particle Physics.
Mail:   1211 Geneve 23, Switzerland          Phone: +41 22 7679248
E-Mail: Fons.Rademakers@cern.ch              Fax:   +41 22 7677910



This archive was generated by hypermail 2b29 : Tue Jan 04 2000 - 00:26:19 MET