Hi,
I’m working with ROOT 5.12 and experience a problem related with fetching a TVectorT from a TObjArray.
I want to fill a TObjArray inside some function create(TObjArray* a). The elements of the TObjArray are of type TVectorT.
Then I want to retrieve the values of the TVectorT inside the TObjArray.
void call() {
TObjArray* a = new TObjArray();
create( a );
cout << "CCC: " << ((TVectorT<double>*)a->At(0)) << " "
<< (*((TVectorT<double>*)a->At(0)))[0] << endl;
}
This gives a segmentation violation (I tried both 5.12 and 5.18, same result). The strange thing is that even thought &t0 and ((TVectorT*)a->At(0)) are the same address, I cannot acces the values of the TVectorT outside the create() method.
[quote=“daan”]Hi,
I’m working with ROOT 5.12 and experience a problem related with fetching a TVectorT from a TObjArray.
I want to fill a TObjArray inside some function create(TObjArray* a). The elements of the TObjArray are of type TVectorT.
Then I want to retrieve the values of the TVectorT inside the TObjArray.
void call() {
TObjArray* a = new TObjArray();
create( a );
cout << "CCC: " << ((TVectorT<double>*)a->At(0)) << " "
<< (*((TVectorT<double>*)a->At(0)))[0] << endl;
}
This gives a segmentation violation (I tried both 5.12 and 5.18, same result). The strange thing is that even thought &t0 and ((TVectorT*)a->At(0)) are the same address, I cannot acces the values of the TVectorT outside the create() method.
What can/should I do?
thanks for any help,
daan[/quote]
In the “create” function you store a reference to a TVectorT object, which is created on the local stack of the “create” function. As soon as you go out of the scope of this function your reference is no longer a valid one.
Try to use something like that:
[code] void create( TObjArray* a ) {
TVectorT *t0 = new TVectorT(3);
…
…
a->Add(t0);
…
[/code]
Also you might want to use a->SetOwner() and typedef (at least for TVectorT).