[ROOT] array of pointers

From: Tommaso Chiarusi (Tommaso.Chiarusi@bo.infn.it)
Date: Thu Aug 22 2002 - 21:33:40 MEST


Dear rooters,
I have found this strange behaviour (naturally from my point of view) 
concerning the way cint treats the trasmission of an array of pointers 
with respect to what the g++ compiler does.

I have written the simple function "ReadFile" that reads one text 
datafile (with data organized in columns, each of the same lengths!) 
without caring about its actual length,
retrieving its columns in as many as double arrays, ready to be used for 
example with a TGraph.

As input I need to give only the input filename "fin", the number of 
columns "ncolumns", an array of pointers  "var" dimensioned by the same 
number of the columns, and in the end a initially empty variable for the 
lenght of each column "nrows".

    as an example:

    ifstream fin("file_1.dat");
    int ncolumns = 5;       
    double **var = new double*[ncolumns];
    int nrows=0;
     

At this point I need to call my function, which is

    MyClass::ReadFile(ncolumns,fin,*var,nrows);
                                                           ^^^^
in case of implementation of the function as a method of a class (in my 
case is a class obtained with MakeClass upon a file.root);

while is

   ReadFile(ncolumns,fin,var,nrows);
                                         ^^^   
if I use a non root C++ program, compiled with g++ v. 2.96, where the 
function is set before the main().

The ^^^^ should point to the difference.


the definition of the function is as  the same as in both the cases, i.e.


void ReadFile( int& ncolumns, ifstream&  file, double** &var, int& nrows)
{ 
  double x = 0;
  vector<double> *v = new vector<double>[ncolumns];
 
  while(!file.eof())
    {
      for(int i=0; i<ncolumns;i++)
    {
      file>>x;
        v[i].push_back(x);
    }
    }   

  nrows = v[0].size()-1;
 
  double *ar;
 
  for(int j=0;j<ncolumns;j++)
    {
      ar = new double[nrows];
      for(int k=0;k<nrows;k++)
    {
      ar[k]=v[j][k];
    }

      var[j]=ar;
    }
 
 
}

    If I take care to use such different notation "*var" or "var" 
according to  cint or g++ I get no problem, and everyone is happy.
But if I use with cint the notation valid for g++ I get an error.

So , what do I ignore about C++?
Is this normal?


Cheers, Tommaso  

Further, in the cint case, if I give the same call as in the program I get



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