A strangeness for init value for root prompt


_ROOT Version: 6.30.02
_Platform: macOS


cx[004]:>  uint8_t aa = 0x3a
(unsigned char) ':'
cx[005]:> aa
(unsigned char) ':'
cx[006]:> (int)aa
(int) 58
cx[007]:> cout<<(int)aa<<endl;
58
cx[008]:> cout<<int(aa)<<endl;
58
cx[009]:> int(aa)            //  this line re-init aa=0
(int) 0

Of course, this is not a bug.

Hi @cxwx1 ,

Thanks for reaching out on the forum!

The snippet you report shows a mix of normal C++ behaviour and cling features:

  • The normal C++ behaviour is in that int(aa) does not do what you think. That is a (re-)declaration of the variable aa that you declared initially. In any other C++ context, you would get a compilation error → Compiler Explorer
  • The fact you see the value 0 assigned to aa (which makes it look like a re-definition of the same variable) is actually due to the fact that cling allows re-declaration of the same variable-name, but internally it is actually a different variable for the compiler. This is a feature quite important for interactive user sessions in C++.:
root [1] std::uint8_t aa = 0x3a
(unsigned char) ':'
root [2] &aa
(std::uint8_t *) 0x7f6b367fa000
root [3] int(aa)
(int) 0
root [4] &aa
(int (*)) 0x7f6b3667e000 // Notice the change in address!

Bottom line, you are seeing two different aa variables, not the same one being re-initialized.

Cheers,
Vincenzo

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.