[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: 980114: Is set_cur correct?



>To: address@hidden
>From: NARITA Kazumi <address@hidden>
>Subject: Is set_cur correct?
>Organization: Fujitsu
>Keywords: 199801141145.EAA09946 netCDF 3.3

Hi Narita,

>  I'm using the C++ interface to NetCDF version 3.3.1.
>  I have a question about the following function in the netcdf.cc file.
>  I think, if parameters are not -1, this function does nothing.
>  Is that true?
> 
> >NcBool NcVar::set_cur(long c0, long c1, long c2, long c3, long c4)
> >{
> >    long t[5];
> >    t[0] = c0;
> >    t[1] = c1;
> >    t[2] = c2;
> >    t[3] = c3;
> >    t[4] = c4;
> >    for(int j = 0; j < 5; j++) {
> >     int i;
> >     if (t[j] == -1) {
> >         if (num_dims() < j)
> >           return FALSE;     // too many for variable's dimensionality
> >         for (i = 0; i < j; i++) {
> >             if (t[i] >= get_dim(i)->size() && ! get_dim(i)->is_unlimited())
> >               return FALSE; // too big for dimension
> >             the_cur[i] = t[i];
> >         }
> >         for(i = j; i < num_dims(); i++)
> >           the_cur[i] = 0;
> >         return TRUE;
> >     }
> >    }
> >    return TRUE;
> >}

It appears that you have identified a bug in the case that set_cur is
called with exactly 5 parameters (for variables with 5 dimensions).  In
that case, you are correct:  the function does nothing although it is
supposed to set the ith element of the_cur[] to the ith parameter.
Thanks for pointing out the bug!

If the function is called with fewer than 5 parameters, I believe the
function works correctly, since the outer j loop determines how many
parameters appeared in the call (in netcdf.hh, the default values for
all 5 parameters is declared to be -1).  Then the inner i loop sets
the_cur[i] to the ith parameter and the rest to zero, as was intended.

Here's what I believe to be a fixed version.  Please let me know if this
doesn't work as intended, or if you have a better suggestion for fixing
the bug:

NcBool NcVar::set_cur(long c0, long c1, long c2, long c3, long c4)
{
    long t[6];
    t[0] = c0;
    t[1] = c1;
    t[2] = c2;
    t[3] = c3;
    t[4] = c4;
    t[5] = -1;
    for(int j = 0; j < 6; j++) { // find how many parameters were used
        int i;
        if (t[j] == -1) {
            if (num_dims() < j)
              return FALSE;     // too many for variable's dimensionality
            for (i = 0; i < j; i++) {
                if (t[i] >= get_dim(i)->size() && ! get_dim(i)->is_unlimited())
                  return FALSE; // too big for dimension
                the_cur[i] = t[i];
            }
            for(i = j; i < num_dims(); i++)
              the_cur[i] = 0;
            return TRUE;
        }
    }
    return TRUE;
}

Thanks again for reporting the bug.

--Russ

_____________________________________________________________________

Russ Rew                                         UCAR Unidata Program
address@hidden                     http://www.unidata.ucar.edu