Re:

From: Kevin B. McCarty <kmccarty_at_gmail.com>
Date: Fri, 9 Nov 2007 09:19:38 -0800


Hi Annalisa,

On Nov 9, 2007 7:41 AM, Annalisa De Caro <Annalisa.de.Caro_at_cern.ch> wrote:
> Hi rooters,
>
> can anyone explain me what the difference is
> in between the following two line sets:
>
> 1)
> const Float_t fgkAl3parameters[3] = {3., 4., 0.1};
> Float_t *ccc;
> for (Int_t ii=0; ii<3; ii++) ccc[ii] = fgkAl3parameters[ii];

You declared a pointer to a Float_t, but never allocated memory at the location where the pointer points. Then you tried to set the contents of this non-existent memory. Thus the segfault. If you did this in the second line:

Float_t *ccc = new Float_t[3];

it should work OK.

(Of course, then you have the responsibility to delete [] the allocated memory before ccc goes out of scope, in order to avoid a memory leak.)

> 2)
> const Float_t fgkAl3parameters[3] = {3., 4., 0.1};
> Float_t *ccc = &fgkAl3parameters;

At first glance, this is OK because the memory pointed to by ccc is already allocated as the array fgkAl3parameters. But you should really be declaring

Float_t * ccc = fgkAl3parameters;

(without the & operator) instead, since fgkAl3parameters, as an array of Float_t, can already be converted to a pointer to Float_t. It looks like CINT lets this slide, but the compiler is more strict and doesn't permit the error.

Hope this is helpful,

-- 
Kevin B. McCarty <kmccarty_at_princeton.edu>   Physics Department
WWW: http://www.princeton.edu/~kmccarty/    Princeton University
GPG: public key ID 4F83C751                 Princeton, NJ 08544
Received on Fri Nov 09 2007 - 18:20:00 CET

This archive was generated by hypermail 2.2.0 : Fri Nov 09 2007 - 23:50:01 CET