Arrays with variable number of elements in a class

Dear ROOTers,
I created a class with:

[..]
private:
Int_t nData;
Int_t* iChan; //nData
Float_t* fValue; //nData
[..]

and I use the automatic streamer to do the class persistence (I use this class in TTree events)

I changed now the way i fill the variables of this class before inserting it in the tree. Now I don’t know a priori the total number of “nData”, but I add new data one by one, increasing nData each time. To avoid to reallocate the memory every time, i moved the memory allocation in the constructor (in all constructors, also the default one) in the following way (maybe not the best one, but surely the quickest):

iChan = new Int_t[MAX_POSSIBLE_NDATA]
fValue = new Float_t[MAX_POSSIBLE_NDATA]

That means that the object keeps always the same (maximum possible) memory allocated.
I think the automatic streamer should continue writing ‘nData’ number of elements: in fact the size of object on disk (or inside a TTree event) seems not increased.

It seems also to work when i reopen the saved file and read again the TTree, but I can’t understand what is the action of automatic streamer when the object is read every event of the TTree for example during TTree::Draw(): is the default constructor executed before the automatic streamer, so MAX_POSSIBLE_NDATA is allocated, or do the automatic streamer the whole job, allocating only nData elements? (in the second case i obtained what i wanted…)

2nd question: at the moment i don’t need to add new elements when i reopen the file with TTree for analysis. If i reopen the file to update it, could i meet problem due this “shortcut”?

Thx,
Matteo

[quote]is the default constructor executed before the automatic streamer, so MAX_POSSIBLE_NDATA is allocated[/quote]Yes it is.

[quote]or do the automatic streamer the whole job, allocating only nData elements? (in the second case i obtained what i wanted…)
[/quote]But since there is no way for the I/O to know if enough element (>nData) has been allocated for a specific object, it will always reallocate exactly nData .

[quote]2nd question: at the moment i don’t need to add new elements when i reopen the file with TTree for analysis. If i reopen the file to update it, could i meet problem due this “shortcut”? [/quote]I don’t think so.

Philippe

Sry if I insist, but can you confirm my understanding of what happens every time an event is read from a TTree with this class?

  1. The default constructor is executed: here MAX_POSSIBLE_NDATA elements are allocated for iChan and fValue
  2. The automatic streamer is executed: here nData elements are allocated for iChan and fValue
    but this results in a memory leak, isn’t it?

Moreover i want to ask if the automatic generated code is visible in some manner as source code.