Re: [ROOT] ROOT variable scope rules

From: Fons Rademakers (Fons.Rademakers@cern.ch)
Date: Wed Oct 17 2001 - 09:52:19 MEST


Hi Bill,

  check in http://root.cern.ch/root/Cint.phtml?limitations
the item about scope.

Cheers, Fons.



William Hanlon wrote:
> 
> Hi,
> 
> I do not have a copy of the ANSI C++ rules in front of me, but I
> believe that there are some discrepancies between ROOT and standard
> C++ variable scope rules.
> 
> For example, I expect a variable declared in the body of an if
> statement to be defined throughout the body of that statement like:
> 
>           if (int foo = bar(arg))
>             {
>                cout << "foo is " << foo << endl;
>             }
>           else
>             {
>                cout << " foo is 0 " << endl;
>             }
> 
> This does not work in ROOT. The error returned is:
> 
> Warning: Automatic variable Int_tfoo allocated in global scope FILE:test.C LINE:12
> 
> but works exactly as expected when compiled using a c++
> compiler
> 
> Additionally variables declared in a for loop exist outside of the
> loop. Try running the following in ROOT and compile a version using
> your favorite compiler and you will see a big difference between the
> two outputs:
> 
>     int i = 66;
> 
>     for (int i = 0; i < 10; i++)
>       {
>         int k;
> 
>         k++;
> 
>         cout << "in loop k: " << k << endl;
> 
>       }
> 
>     cout << "outside loop i: " << i << endl;
>     cout << "outside loop k: " << k << endl;
> 
> In fact this should not even compile using a compiler since k is not
> declared outside of the loop. Additionally this program run through
> ROOT will say that i is 10, but it should be 66 shouldn't it?
> 
> I have also run into problems with variables not being removed between
> functions calls to the same function in loops.
> 
> Are these variable scoping differences intended, being worked on, or
> am I just wrong about this? I haven't seen anyone else remark about
> this in the roottalk digest.
> 
> Thanks for any comments,
> Bill

-- 
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 7679480



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