Unexpected behavior of nested for loops when loading a macro with .L

Hi, I noticed what seems to be an unexpected behavior in Cling (ROOT 6.34.08, GCC 15.2.0, Linux). The following simple code works correctly when entered directly at the interactive prompt:

for (int i = 0; i < 10; ++i) {
for (int j = 0; j < 10; ++j) {
cout << i << " " << j << endl;
}
}

However, if I put exactly the same code in a file (not inside a void function) and load it with .L test_loop.C I get:

0 0
0 1
0 2
0 3
0 4
0 5
0 6
0 7
0 8
0 9

The inner loop is executed only during the first iteration of the outer loop. Moreover, i and j remain visible in the interpreter:

root [1] i
(int) 10
root [2] j
(int) 10

If I put the same code inside a function and call the function the output is correct.
The same test with ROOT 6.28/08 produces a parsing error (expected unqualified-id) when the parser reaches the first for loop.

Thanks,
Alessandro

It could be a problem with your system or your ROOT. I don’t see the issue with the Ubuntu 24.04 binary of ROOT 6.34.08 (GCC 13.3), on Linux Mint. The ‘official’ download page (Release 63408 - ROOT) does not list any binary compiled with gcc 15.2.0 (the closest would be the MAC OS 15.4 version). You didn’t mention your OS or which ROOT binary (or compiled?), but if you are using a binary, it should be a version compiled with your same GCC version; otherwise, you should compile it yourself.

You try to use the so-called unnamed ROOT macro. Your source code must be enclosed in curly braces (I somehow remember that the “{” needed to be the very first line in an unnamed ROOT macro file):

{
 // ... your source code ...
}

BTW. To load and execute a macro, you should use “.x”, not “.L”.

With the curly braces it works. I thought it was illegal to use a for loop outside of any function (and why do the loop variables survive?). Perhaps the problem is with my own ROOT package: there is no package for Slackware Linux, so I built it myself.

Next time I’ll try the official packages first.

Thanks