Problem with tvectort and tobjarray

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.

void create( TObjArray* a ) { TVectorT<double> t0(3); t0[0] = 0; t0[1] = 1; t0[2] = 2; a->Add(&t0); // cout << "LLL: " << &t0 << " " << a->At(0) << endl; cout << "LLL: "; for ( int i=0; i<(*((TVectorT<double>*)a->At(0))).GetNoElements(); i++ ) { cout << (*((TVectorT<double>*)a->At(0)))[i] << " "; } cout << endl; return; }

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=“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.

void create( TObjArray* a ) { TVectorT<double> t0(3); t0[0] = 0; t0[1] = 1; t0[2] = 2; a->Add(&t0); // cout << "LLL: " << &t0 << " " << a->At(0) << endl; cout << "LLL: "; for ( int i=0; i<(*((TVectorT<double>*)a->At(0))).GetNoElements(); i++ ) { cout << (*((TVectorT<double>*)a->At(0)))[i] << " "; } cout << endl; return; }

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).

thanks, this makes sense. it works now

i’m not an expert, what can i gain by using typedef (and how should i do it?)

i’m not an expert, what can i gain by using typedef (and how should i do it?)[/quote]

Your code will look nicer and will be supported easier :slight_smile:
For example, if you would have:

typedef TVectorT<double> DblVector_t; or something.

msdn.microsoft.com/en-us/library/05w82thz(VS.80.aspx
functionx.com/cpp/Lesson19.htm
look for “typedef Specifier” in www.google.com

Hi Daan,

Have a look at:

root.cern.ch/root/html/MATH_MATRIX_Index.html

and in particular:

root.cern.ch/root/html/TVectorD.html

which contains

typedef TVectorT TVectorD

Eddy