Re: Pointer Issues

From: Stephen J. Gowdy <gowdy_at_slac.stanford.edu>
Date: Tue, 21 Jun 2005 22:08:50 -0700 (PDT)


I think this is just a C++ issue. You want;

void getHIST(TH1F*& hist, int i)

so that the original pointer in your get() fuction gets modified. I don't know if the pointer will continue to be valid after end of getHIST(...) though, it depends on what the Get(emcobj) method does.

On Tue, 21 Jun 2005, Christopher James Schilling wrote:

> Hi, I have the following, relatively simple piece of code that I am
> trying to run in Root, V4.03/02 on SL3.
>
> #include "TFile.h"
> #include "TString.h"
> #include "TH1.h"
> #include "iostream.h"
>
> TFile* f=new TFile("./DAT/slg_ecalc.root");
>
> void getHIST(TH1F* hist, int i) {
> TString emcobj;
> emcobj = "emc1"; emcobj+=i;
> hist = (TH1F*)f->Get(emcobj);
> //hist->Draw();
> }
>
> void get() {
>
> TH1F* hist;
> getHIST(hist, 2);
> cout << " hist = " << emechist << endl;
> hist->Draw();
>
> }
>
> If I issue the command
> >>> root -l get.C -q
> I get the following output
>
> emechist = 0
> Error: illegal pointer to class object emechist 0x0 191 FILE:get.C LINE:20
>
> If I issue the command (trying to Compile)
> >>> root -l get.C+ -q
> I get a segmentation violation:
>
> emechist = 0
>
> *** Break *** segmentation violation
> Generating stack trace...
> 0x004e5b02 in <unknown> from /u2/schilling/slg/./get_C.so
>
> It seems as if the histogram is out of scope outside the function. The
> funny thing is if I change
> void getHIST(TH1F* hist, int i) to void getHIST(TH1F&* hist, int i)
> the macro works, and the histogram is drawn, but I cannot compile since
> it is illegal to have a pointer to a reference.
>
> Any ideas??
> Cheers
> Chris S
>

--
 /------------------------------------+-------------------------\
|Stephen J. Gowdy                     | SLAC, MailStop 34,       |
|http://www.slac.stanford.edu/~gowdy/ | 2575 Sand Hill Road,     |
|http://calendar.yahoo.com/gowdy      | Menlo Park CA 94025, USA |
|EMail: gowdy_at_slac.stanford.edu       | Tel: +1 650 926 3144     |
 \------------------------------------+-------------------------/
Received on Wed Jun 22 2005 - 07:08:59 MEST

This archive was generated by hypermail 2.2.0 : Tue Jan 02 2007 - 14:45:09 MET