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

Re: 19990325: c++ interface bug? NcFile::get_var()



Ionutz,

>Date: Fri, 26 Mar 1999 12:07:45 +0900
>From: Ionutz Borcoman <address@hidden>
>Organization: .
>To: Steve Emmerson <address@hidden>
>Subject: Re: 19990325: c++ interface bug? NcFile::get_var()
>Keywords: 199903251256.FAA21940

In the above message, you wrote:

> No, what I am saying is that the program simply CRASH, which is as bad
> as sending a non-null pointer. There is no function in the NcFile class
> to test by name the existence of a var/atrib/dim. From my point of view,
> the behaviour should be like this:
> - if I use NcFile::get_var(NcToken name) and send a name which can be
> found in the NcFile, a NULL pointer should be returned. Instead, the
> program crash.
> 
> I have attached a test example taht uses the Example.nc generated by
> your example.cc program. With my extended program I get a sigsegv, not
> "illegal instruction". Here is the output of the attached program:
> 
> bash-2.02$ ./a.out
> variable's name is P
> Illegal instruction
> 
> TIA,
> 
> Ionutz
> --------------DF1E69454F28A2F62809DE72
> Content-Type: text/plain; charset=us-ascii;
>  name="example3.cc"
> Content-Transfer-Encoding: 7bit
> Content-Disposition: inline;
>  filename="example3.cc"
> 
> #include <string.h>
> #include <time.h>
> #include <stdlib.h>
> #include "netcdf.hh"
> 
> int main()
> {
>       NcFile *nc = new NcFile("Example.nc", NcFile::ReadOnly);
>       // check if the file was opened
>       if( !nc->is_valid() ){
>         delete nc;
>         exit(1);
>       }
>       NcVar* var = nc->get_var("P");
>       if(var)
>         printf("variable's name is %s\n", var->name());
>       var = nc->get_var("xxx");
>       if(!var)
>         printf("not existent variable\n");
>       printf("Exit normally\n");
>       delete nc;
> }

The default action of the netCDF library upon encountering an error
(such as a non-existant variable) is to print an error message and exit
with an error status -- which is exactly what happens in your example
program.  If you do not want the default behavior, then you must use the
NcError class to set the behavior you want.  For example, your program
could be modified to make the netCDF library ignore all errors by adding
the following line to the top of the body of main():

    NcError* ncErr = new NcError(NcError::silent_nonfatal);

If this is done, then your program prints the following:

    not existent variable
    Exit normally

--------
Steve Emmerson   <http://www.unidata.ucar.edu>