Re: [ROOT] Adjustable array dimension

From: Victor Perevoztchikov (perev@bnl.gov)
Date: Thu Mar 08 2001 - 17:23:09 MET


Hi Radovan,

> > Int_t fun(const Int_t n, Int_t x) {
> >    Int_t arr[n];

such array is forbidden by C++. Even if CINT can handle
it, it is better to avoid non C++ constructions

Victor

Radovan CHYTRACEK wrote:
> 
> Here is my version which works:
> 
> Int_t fun(const Int_t n, Int_t x) {
>    Int_t* arr = new Int_t[n];
>    arr[n-1] = x;
>    Int_t ret = arr[n-1];
>    delete [] arr;
>    return ret;
> }
> 
> void pass(Int_t k, Int_t x) {
>    for (Int_t i=1; i<=k ; i++)
>       cout << "fun(" <<i<<","<<x<<") = " << fun(i,x) << endl;
> }
> 
> I think that Masa Goto is now hard thinking about this problem.
> 
> Radovan
> 
> Jiri Bystricky wrote:
> >
> >   Hi,
> >   is anything wrong with the script 'test.C' (see below)?
> > It gives (ROOT version  3.00/02):
> >
> > root [0] .L test.C
> > root [1]  cout << fun(1,5) << endl;
> > 5
> > root [2]  cout << fun(2,5) << endl;
> > 5
> > root [3] pass(2,5)
> > fun(1,5) = 5
> > Error: Array index out of range arr -> [1]  valid upto arr FILE:test.C LINE:8
> > fun(2,5) = 0
> > *** Interpreter error recovered ***
> > root [4]  cout << fun(2,5) << endl;
> > Error: Array index out of range arr -> [1]  valid upto arr FILE:/tmp/filevtC7Yq_cint
> > LINE:2
> > Error: Illegal pointer operation (tovalue) FILE:/tmp/filevtC7Yq_cint LINE:1
> > *** Interpreter error recovered ***
> >
> >     Cheers   Jiri
> > -----------------------------------------------------------------------------
> >   test.C
> >
> > Int_t fun(const Int_t n, Int_t x) {
> >    Int_t arr[n];
> >    arr[n-1] = x;
> >    return arr[n-1];
> > }
> > void pass(Int_t k, Int_t x) {
> >    for (Int_t i=1; i<=k ; i++)
> >       cout << "fun(" <<i<<","<<x<<") = " << fun(i,x) << endl;
> > }
> > -----------------------------------------------------------------------------
> 
>   ------------------------------------------------------------------------------------------------------------------------------------
>                                Name: Radovan.Chytracek.vcf
>    Radovan.Chytracek.vcf       Type: VCard (text/x-vcard)
>                            Encoding: base64
>                         Description: Card for Radovan CHYTRACEK

-- 
Victor M. Perevoztchikov   perev@bnl.gov  perev@vxcern.cern.ch       
Brookhaven National Laboratory MS 510A PO Box 5000 Upton NY 11973-5000
tel office : 631-344-7894; fax 631-344-4206; home 631-345-2690



This archive was generated by hypermail 2b29 : Tue Jan 01 2002 - 17:50:39 MET