Re: [ROOT] array of pointers ...

From: Andreas WILDAUER (andreas.wildauer@cern.ch)
Date: Thu Jul 17 2003 - 15:13:12 MEST


Hi Rene,

thanks for your quick answer.
Nevertheless it's not solved yet:

I have a return function which returns a pointer to an array of 4 floats

in my CONFIG class the original 2-d array is:
Float_t m_delta[4][4];

and the pointer returned by the member function is: m_delta[0]  (for example)

so, in my CALCULATE class I need a pointer like this (I thought):

Float_t * m_delta;
which then is "assigned" to point to the array of 4 floats
m_delta = (member function of config class which returns delta[0]);
and in this case: m_delta[2] in CALCULATE has the value of m_delta[0][2] of 
CONFIG

ok, if this doesn't work because of the reasons you mentioned, I tried
Float_t m_delta[4];

so it is for sure that ROOT knows m_delta is a pointer to an array containing 
4 floats.
But now:

m_delta = (member function, ... see above);
gives me the following error message:

Error: Incorrect assignment to m_delta_eta_Si, wrong type 'Float_t*' 
FILE:Level2IDandIDCalo.C LINE:138
(I changed var names in my example so it is shorter)
That was the way I first tried, and I still don't understand the error since 
the member funciton returns a pointer and m_delta is a pointer.

Since I only need "one 4 array" in my CALCULATE class out of the 4by4 array 
defined in CONFIG theres no use to define:

Float_t m_delta[4][4];

in my calculate class. (Out of "desperation" I tried this as well and get even 
more errors.)

I hope it's not me beeing stupid ...
Thanks in advance again for an additional hint ...

Andi W

(
Add. info (not so important): it always works if the starting point is a 1-d 
array:

CONFIG:
Float_t m_delta[4];

Float_t * returndelta() {return m_delta;}

CALCULATE:
Float_t * m_delta;
m_delta = returndelta();
cout << m_delta[2] << endl;

here it also doesnt know how "long" the array is *m_delta  points to.
)

On Thursday 17 July 2003 14:15, Rene Brun wrote:
> Hi Andreas,
>
> A declaration like
>   Float_t * m_delta;
>
> in a class is assumed to be a variable length array of floats by default.
> To do I/O with this class, Root needs to know the effective length
> of the array. A declaration like
>   Float_t * m_delta;  //[m_len]
> is required, where m_len is a data member of the class with the effective
> length of then array.
> When reading your class "Calculate", Root will automatically delete
> th array m_delta and recreate a new array with the length m_len.
>
> If your array is a fix length array (1-d, 2-d or more) you should use
>   Float_t   m_delta[4][4];
> instead of the pointer.
>
> Rene Brun
>
> Andreas WILDAUER wrote:
> > Hi everybody,
> >
> > I have quite a strange problem:
> >
> > in class CONFIG I have:
> >
> > config.h: ***************
> > private:
> > Float_t m_delta[4][4];
> >
> > public:
> > Float_t * delta(int dset);
> >
> > config.C: ****************
> >
> > m_delta[0][0] = 0.07;
> > m_delta[0][1] = 0.06;
> > m_delta[0][2] = 0.05;
> > m_delta[0][3] = 0.05;
> > a.s.o.
> >
> > Float_t * config::delta(Int_t dset) { return m_delta[dset];}
> >
> > where m_delta[0] for example is a pointer on the four Float_t's
> > m_delta[0][0-3]
> > *******************************************************
> > in the CALCULATE class I have:
> >
> > calculate.h: *******************
> > Float_t * m_delta;
> >
> > and in calculate.C: *************
> > config configobj;
> > m_delta = configobj.delta(0);
> >
> > So far m_delta should be a pointer on 4 Float_t's, namely:
> > m_delta[0][0-3] which were defined in conifg.C.
> > ********************************************************
> > now I also have a printvalues member function in class CALCULATE.
> > the code in MAIN program main.C is the following:
> >
> > calculateobj.printvalues();
> > cout << "GetEntries" << endl;
> > fChain->GetEntry(jentry);
> > calculateobj.printvalues();
> >
> > where fChain is a private data member of the MAIN class (which also has
> > all the ntuple variables as usual (from setMakeClass)).
> > if i load main.C (not compiling) the output is:
> >
> > m_delta[0] = 0.07
> > m_delta[1] = 0.06
> > m_delta[2] = 0.05
> > m_delta[3] = 0.05    // CORRECT
> > GetEntries
> > m_delta[0] = 9.3887e-44
> > m_delta[1] = 2.71071e-33
> > m_delta[2] = nan
> > m_delta[3] = 2.7111e-33  // ?????????????????????????
> >
> > ????????????????????????
> > how  can fChain->GetEntry(jentry); change the memory where the pointers
> > m_delta[0-3] point to (which are private data members of another class,
> > namely CALCULATE)
> >
> > or is it wrong to assume that array[0] is a pointer when the array is
> > defined as array[4][4] (like array1 is a pointer on array1[27], what I
> > also use and it works ...)
> >
> > Please help
> > Thanks in advance
> > Andi Wildauer



This archive was generated by hypermail 2b29 : Thu Jan 01 2004 - 17:50:13 MET