# Wrong object returned in ROOT when using auto keyword

**URL:** <https://root-forum.cern.ch/t/wrong-object-returned-in-root-when-using-auto-keyword/46188>\
**Category:** TMVA\
**Created:** [August 5, 2021, 6:41pm UTC](https://root-forum.cern.ch/t/wrong-object-returned-in-root-when-using-auto-keyword/46188 "2021-08-05T18:41:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bleu65](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bleu65](https://root-forum.cern.ch/u/bleu65)\
**Post date:** [August 5, 2021, 6:41pm UTC](https://root-forum.cern.ch/t/wrong-object-returned-in-root-when-using-auto-keyword/46188/1 "2021-08-05T18:41:26Z")

</div>

If you use the keyword auto in the interpreter, it doesn’t correctly return (or at least print out the pointer) of the correct object. Bit of a gotcha.

For example, I have two instances of a class called Model loaded from two open files. I have a DataLoader class that handles the branch addresses etc. In any case, `new DataLoader()` creates a new member `Model*`.

```auto
root [1] DataLoader* dl = new DataLoader("r11-test3.root")
(DataLoader *) 0x7fe5e5485bf0
root [2] DataLoader* dl2 = new DataLoader("r11-test3b.root")
(DataLoader *) 0x7fe5e54dedc0
root [3] auto mt = dl->GetModelTree()
(TChain *) @0x7ffeed30a038
root [4] auto mt2 = dl2->GetModelTree()
(TChain *) @0x7ffeed30a038 //< WRONG
root [5] mt->GetEntry(0)
(int) 2085
root [6] mt2->GetEntry(0) //< which one am I accessing?
(int) 2085
root [7] auto mod = dl->GetModel()
(Model *) @0x7ffeed30a038
root [8] auto mod2 = dl2->GetModel()
(Model *) @0x7ffeed30a038 //< WRONG

```

The second one gets the wrong object (on input 4). Using an explicit type though (same session, same variables), it does:

```auto
root [16] Model* mod4 = dl2->GetModel()
(Model *) 0x7fe5e54e08c0 //< CORRECT
root [17] TTree* mt2b = dl2->GetModelTree()
(TTree *) 0x7fe5e54e8640 //< CORRECT

```

In this case, I expect the data to be the same so I expect the same number of bytes to be loaded from GetEntry. So it’s ambiguous what I’m accessing.

```auto
root [19] mt2
(TChain *) @0x7ffeed30a038
root [20] mt
(TChain *) @0x7ffeed30a038
root [21] mt2b
(TTree *) 0x7fe5e54e8640

```

Perhaps this is intended behaviour? But it would be wrong in C++.

_ROOT Version:_ 6.24/02  
_Platform:_ Mac OSX 11.4  
_Compiler:_ Apple clang version 12.0.5 (clang-1205.0.22.11) Target: x86\_64-apple-darwin20.5.0

* * *

---

<div class="post-metadata">

**Author:** ![jonas](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jonas/32/24756_2.png) [@jonas](https://root-forum.cern.ch/u/jonas)\
**Post date:** [August 5, 2021, 8:45pm UTC](https://root-forum.cern.ch/t/wrong-object-returned-in-root-when-using-auto-keyword/46188/2 "2021-08-05T20:45:15Z")

</div>

Hi @bleu65,

thanks for reporting this strange problem! I have opened a GitHub issue for it with a simple reproducer:

> <https://github.com/root-project/root/issues/8806>
>
> \- \[\] Checked for duplicates
> 
> When the \`auto\` keyword is used in the interpret…er to substitute pointer types, the value of the pointer is not printed out correctly, even though the pointer points to the correct variable.
> 
> A few simple interpreter commands to reproduce the problem:
> \`\`\`
> root \[0\] int x = 1
> (int) 1
> root \[1\] int y = 2
> (int) 2
> root \[2\] auto xp = &x
> (int \*) @0x7ffe114d3bb8 \<- WRONG!
> root \[3\] auto yp = &y
> (int \*) @0x7ffe114d3bb8 \<- WRONG!
> root \[4\] &x
> (int \*) 0x7f3bcb682018
> root \[5\] &y
> (int \*) 0x7f3bcb68201c
> root \[6\] xp
> (int \*) @0x7ffe114d3bb8 \<- WRONG!
> root \[7\] yp
> (int \*) @0x7ffe114d3bb8 \<- WRONG!
> root \[8\] \*xp
> (int) 1
> root \[9\] \*yp
> (int) 2
> \`\`\`
> 
> However, if one uses \`auto \*\` instead of auto, one gets the expected behavior:
> 
> \`\`\`
> root \[0\] int x = 1
> (int) 1
> root \[1\] int y = 2
> (int) 2
> root \[2\] auto \* xp = &x
> (int \*) 0x7f8f0a394018 \<- CORRECT!
> root \[3\] auto \* yp = &y
> (int \*) 0x7f8f0a39401c \<- CORRECT!
> root \[4\] &x
> (int \*) 0x7f8f0a394018
> root \[5\] &y
> (int \*) 0x7f8f0a39401c
> root \[6\] xp
> (int \*) 0x7f8f0a394018 \<- CORRECT!
> root \[7\] yp
> (int \*) 0x7f8f0a39401c \<- CORRECT!
> root \[8\] \*xp
> (int) 1
> root \[9\] \*yp
> (int) 2
> \`\`\`
> 
> Can we fix the interpreter such that it also prints the correct pointer values when \`auto\` is used?
> 
> This problem was originally spotted on the ROOT forum:
> https://root-forum.cern.ch/t/wrong-object-returned-in-root-when-using-auto-keyword

It seems that the pointer are actually still pointing to the correct object, it’s just that the interpreter prints out something wrong. So you can proceed with `auto` if you don’t care about the prints, or just use `auto *` which works as expected. So I hope you can continue working with that until one of our interpreter experts will take care about the issue on GitHub?

Cheers,  
Jonas

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [August 19, 2021, 8:45pm UTC](https://root-forum.cern.ch/t/wrong-object-returned-in-root-when-using-auto-keyword/46188/3 "2021-08-19T20:45:37Z")

</div>

Closed on August 19, 2021, 8:45pm UTC

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