Strict-aliasing problem in Streamer() for class using enum

When trying to compile simple class with Streamer() generated by rootcint:

rootcint -f A_dict.cc -c A.h ALinkDef.h 
g++ -c A_dict.cc -o A_dict.lo -Wall -I/cern/root_v5.20.00-1/include -O2

I obtain such warning:

A_dict.cc: In member function ‘virtual void A::Streamer(TBuffer&)’:
A_dict.cc:114: warning: dereferencing type-punned pointer will break strict-aliasing rules

compiler -02 options includes -fstrict-aliasing so the definition of streamer:

void A::Streamer(TBuffer &R__b)
{
   // Stream an object of class A.

   UInt_t R__s, R__c;
   if (R__b.IsReading()) {
      Version_t R__v = R__b.ReadVersion(&R__s, &R__c); if (R__v) { }
      TObject::Streamer(R__b);
      R__b >> (Int_t&)typ;
      R__b.CheckByteCount(R__s, R__c, A::IsA());
   } else {
      R__c = R__b.WriteVersion(A::IsA(), kTRUE);
      TObject::Streamer(R__b);
      R__b << (Int_t)typ;
      R__b.SetByteCount(R__c, kTRUE);
   }
}

produces this warning because of this line:

R__b >> (Int_t&)typ;

How should I generate _dict file with LinkDef pragma’s to have it properly done ?
I am not interested in solution with -fno-strict-aliasing option given to compiler.

My root version is 5.20.

Thans in advance!
Marcin
ALinkDef.h (197 Bytes)
A.h (132 Bytes)

Hi,

The Streamer function, as you have it, is obsolete; instead you should request the new style of streamer function by using in your LinkDef.h:#pragma link C++ class A+;

Cheers,
Philippe.

That old Streamer works in my software when new one generates Segmentation Fault … So I must check it again why is that.

I found another problem with strict-aliasing when dealing with stl’s map and pair.

There is typedef in class C:

class C : public TNamed  {
 public:
  C() : TNamed() {}
  typedef std::map<int, B*> t_m;
 private:
  ClassDef(C,1)
};

which is a source of strict-aliasing warnings:

rootcint -f C_dict.cc -c C.h CLinkDef.h 
g++ -c C_dict.cc -o C_dict.lo -Wall -I/cern/root_v5.20.00-1/include -I ./ -O2 

C_dict.cc: In function ‘int G__C_dict_345_0_2(G__value*, const char*, G__param*, int)’:
C_dict.cc:837: warning: dereferencing type-punned pointer will break strict-aliasing rules
C_dict.cc:839: warning: dereferencing type-punned pointer will break strict-aliasing rules

I found that when I overwrite pair’s copy constructor:

namespace std {
  template <> class pair<int, B*> {
    pair(const pair &) {};
  };
}

warnings are not present anymore. I guess that is not easy to correct that problem with pragmas’ in LinkDef file. What should I do ?

Thanks,

Marcin
CLinkDef.h (282 Bytes)
C.h (335 Bytes)

Hi,

My apologies for the late answer.

I am unable to reproduce your last problem. Which platform are you running on? Do you still see the problem with a newer version of ROOT (v5.25/04)?

Cheers,
Philippe.

The dictionary file (C_dict.cc) created by rootcint within both root versions 5.20 and 5.24 has the same part of code:

     p = new pair<int,B*>(*(int*) G__Intref(&libp->para[0]), libp->para[1].ref ? *(B**) libp->para[1].ref : *(B**) (&G__Mlong(libp->para[1])));
   } else {
     p = new((void*) gvp) pair<int,B*>(*(int*) G__Intref(&libp->para[0]), libp->para[1].ref ? *(B**) libp->para[1].ref : *(B**) (&G__Mlong(libp->para[1])));

I do not know what G__Mlong is but dereferencing it breaks strict-aliasing rules… but life is not so simply and that warning is visible with gcc 4.1.2 (debian 3.4.6-5). I tested compilation of the same C_dict.cc with 3.4.6. & 4.3.2 and did not see any warning.

So maybe first check dictionary file created with the newest rootcint and decide if it is real warning or fake for one compilator version.

Marcin

Warning is also visible with gcc 4.2.2

I cannot reproduce your problem with any ROOT version or compiler version. May be you have some pathological include in your local directory.
Could you replace:

by

Rene

I guess it is not a problem of includes. Tried your modification but without positive result.

In C_dict.cc generated with rootcint I have 2 places in which dereferencing of G__Mlong is applied : (&G__Mlong(libp->para[1]))). In dictionary file included to this post you can try to find them and check if in dictionary files generated by your rootcint they still exist or not.

Please submit your dictionary file created for class C.

Marcin
C_dict.cc.gz (6.9 KB)

My file C_dict.cc is identical to your file and I do not see any warning/error generated by the compiler (I tried with gcc4.3 and 4.4)

Rene

Since the newest version of gcc 4.3.2 (and 4.4) does not show any problem, I am assuming that this was a ‘deficiency’ in gcc.

Cheers,
Philippe.