Interesting Behavior from "equivalent" statements...

Pasha Murat (murat@cdfsga.fnal.gov)
Wed, 31 Dec 1997 17:21:19 GMT


Tim Miller writes:
> Actually there are 2 strange behaviors. The first (brief)
> one is this:
>
> TRQMD FileList = new TRQMD(filename,10);
> (FileList+0)->function(); // Root Seg fault
> TRQMD *temp = FileList + 0; temp->function(); // Root ok
>

Hi Tim,

as your `FileList' var is a pointer you should type

TRQMD* FileList = new TRQMD(filename,10);
^
For me this looks like an argument for CINT not to allow "extentions" to C++.
May be Masaharu could comment more on this.

I also suspect that your second problem is of the same nature: if you are not
reassigning the value of the pointer (which presumably is *not* declared as
a pointer) everything is OK.
Regards, Pasha.

--------------------------------------------------------------------------------
> The second strange behavior is more complicated....
>
> Background: I've compiled my own "dll" (.so for linux)
> with 3 classes to use for my research. Since this is
> my first real attempt at classes (I'm mainly a C guy),
> the heirarchy may not be the greatest (*shrug*). One
> class for the file (and an array of pointers to the
> events in the file), one class for the event (with an
> array of pointers to the particles), and one class for
> the particles (with Int_t and Float_t for 4P, mass, and
> PID).
>
> The best way I can describe the problem is thus (and this
> may be a generic C++ thing, for which I apologize for my
> newbie-ness):
> A Class created by
> Files = new TRQMD[NumFiles];
> with a subsequent call to the overloaded constructor
> TRQMD *TempRQMD = Files + x; // x is loop var
> TempRQMD->TRQMD(filename, NumEvents);
>
> does *NOT* work the same as:
> TRQMD *File = new TRQMD(filename, NumEvents);
> assuming NumFiles == 1 above.
>
> Now, here is the funny part: Both code sequences ACTUALLY
> read the file as it should!!!!(in the TRQMD(char *,int) call)
> However, when I try to access the TempRQMD class functions,
> they return member values that were set with TRQMD() despite the
> TRQMD(filename, NumEvents) call. Lemme illustrate with the
> command line entries that I got from root:
>
> gSystem.Load("stage1.so");
> FileList = new TRQMD[1]; // Same results is no [1]
> TRQMD *TempRQMD = FileList;
> TempRQMD->TRQMD("central-500evts-1.ascii", 10);
> // Normal output from the function returning the
> // Correct values read from the file
> TempRQMD->GetEvents() // Should return 10...as passed in prev.
> -->Root returns this: (Int_t)(-1)
> -->Which is the value set by TRQMD() (from new construction)
>
> However, this is the alternate output:
> gSystem.Load("stage1.so");
> FileList = new TRQMD("central-500evts-1.ascii", 10);
> FileList->GetEvents()
> -->Root returns this: (Int_t)10 // This is right!
>
> Ok, as promised, here is some source code:
>
> (This is from TRQMD.cxx)
> TRQMD::TRQMD() {
> fTotalEvents = -1;
> fEventList = 0;
> }
>
> TRQMD::TRQMD(Char_t *filename, Int_t eventnum) {
> // Some temp var declarations omitted
>
> fTotalEvents = eventnum;
>
> cout << "Eventnumber is " << fTotalEvents << endl;
> // In root this prints out as 10!!!!
>
>
> // rest of file read in code removed.
> }
>
>
>
> --
> ~~ WWW Page: http://quantum.phy.vanderbilt.edu/~tim/ ~~
> Tim Miller | "Am I mature enough to get old?"
> Vanderbilt University | --Me.
> Graduate Studies in Physics |
> Relativistic Heavy Ions |
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~