[ROOT] RE: KCC and ROOT incompatibility??

From: Philippe Canal (pcanal@fnal.gov)
Date: Thu Apr 04 2002 - 02:04:34 MEST


Hi Simon,

I think (I did not per see test on your code) that what you discovered is the well documented
randomness of the initialization and memory allocation.  

Unless you are using ROOT library libNew, the memory that is allocated for an object
is expected to be 'unpredictably' initialized before the execution of the constructor.

In general, the allocation return just the first available piece of memory that is big
enough to the hold the object (when using libNew you are insured that it is also zeroed).

Anyway in the case of your KCC run, what happen is that the memory allocation algorithm just
decided that the space previously occupied by the 1st object (and not free to be reused) is
a good candidate for the 2nd object.  So the 2nd object is allocated in EXACTLY the same
place as the 1st one and the memory __happen__ to be initialized to the last value of
the 1st object .... 

In other word doing:

  randomclass::randomclass(){
     if (randomvariable){

is a bad idea because 'randomvarialbe' will indeed be 'unpredictable' (i.e. function
of whatever happened before + a few more variables !! ).

In a constructor you need to initilized ALL the datamembers of your class without any
regard with a (non-sensical) previous values.

Cheers,
Philippe.

-----Original Message-----
From: Simon Dean [mailto:sdean@hep.man.ac.uk]
Sent: Wednesday, April 03, 2002 5:45 PM
To: Philippe Canal
Cc: roottalk@pcroot.cern.ch
Subject: KCC and ROOT incompatibility??


OK, thanks a lot Philippe and Brett. That sorts out my problem. However, I
think I've uncovered a compatibility problem with KCC on linux.

I made a small class called randomclass - here is the code:

------- In randomclass.hpp -------
class randomclass : public TObject{
private:

  int randomvariable;

public:

  randomclass();
  ~randomclass();

  ClassDef(randomclass,0)  

};

-------- In randomclass.cpp ------
ClassImp(randomclass)

randomclass::randomclass(){
  if (randomvariable){
    cout << "randomclass constructor:" << endl;
    cout << "randomvariable = " << randomvariable << endl;
  }
  randomvariable = 0;
}

randomclass::~randomclass(){
  randomvariable = 5;
  cout << "randomclass destructor:" << endl;
  cout << "randomvariable = " << randomvariable << endl;
}


I made a small program, here is the code: 


int main(int argc, char **argv){
  
  randomclass *random;
  random = new randomclass();
  delete random;
  random = new randomclass();

  return 0;

}


When I build this program with g++ and run it, I get the
(expected) output:

randomclass destructor:
randomvariable = 5

However, when I build it with KCC and run it, I get the
(unexpected) output:

randomclass destructor:
randomvariable = 5
randomclass constructor:
randomvariable = 5

!!!!! randomvariable seems to have 'survived' the delete and new
statements!!!!!!

If anyone has made it this far, is not asleep and wishes to reproduce my
results then I have included the files with this e-mail. Just save
everything in a test directory, in Makefile set THISFILENAME (on line
18) to either 'linuxkcc' for KCC or 'linux' for g++ compilation, then type
'make'.

This will make an executable called messingaround which (should!) give the
above results,

cheers,

Simon



This archive was generated by hypermail 2b29 : Sat Jan 04 2003 - 23:50:48 MET