Multiple identifiers for histogram pointers

I wanted to have a list of histogram pointers (to be filled later), and CINT has problems parsing it. I have the following script:

{
TH1D* a1;
TH1D* b2;
TH1D* c3, d4, e5;

delete a1;
delete b2;
delete c3;
delete d4;
delete e5;
}

When I execute it (with Trace on), I get:

root [1] .x test2
{
2 TH1D* a a1;
3 TH1D* b b2;
4 TH1D* c c3, d4,
!!!Calling default constructor 0xffffffff.TH1D() for declaration of d4 e5;
!!!Calling default constructor 0xffffffff.TH1D() for declaration of e5
5
6 delete a1;
!!!Calling destructor 0x1.~TH1D() for a1
7 delete b2;
!!!Calling destructor 0x1.~TH1D() for b2
8 delete c3;
!!!Calling destructor 0x1.~TH1D() for c3
9 delete d4;Error: d4 cannot delete FILE:test2 LINE:9

!!!Calling destructor 0x8e47538.~TH1D() for e5 ary0:link-1
!!!Calling destructor 0x8e47218.~TH1D() for d4 ary0:link-1*** Interpreter error recovered ***

Is this a documented feature, that I overlooked? I just lost a weekend trying to figure out where the segmentation fault was coming from. :frowning: (I could reproduce the problem both on the Linux Redhat computer and my Mac OSX laptop.)

Your problem has nothing to do with ROOT but with basic C++
Writing
TH1D* c3, d4, e5;
is probably not what you intend !!. You probably want to declare
TH1D *c3, *d4, *e5;
otherwise your statement is equivalent to
TH1D *c3;
TH1D d4;
TH1D e5;

Your code has a second problem. You should initialize your pointers to 0, ie
TH1D *c3=0, *d4=0, *e5=0;
otherwise the pointers will ne uninitialized and you will have a problem with the delete statement

Rene

[quote=“brun”]You probably want to declare
TH1D *c3, *d4, *e5;
otherwise your statement is equivalent to
TH1D *c3;
TH1D d4;
TH1D e5;[/quote]

Doh! It all makes sense now, including why it was calling the default constructor. Thanks. :slight_smile:

In my original code (before it was reduced to this small version), I was using “new TH1D” to initialize the pointers. Even for the ones that were later in the list (like d4). The first sign of any problem was when the script ended, and I got the segmentation fault.