This archive contains answers to questions sent to Unidata support through mid-2025. Note that the archive is no longer being updated. We provide the archive for reference; many of the answers presented here remain technically correct, even if somewhat outdated. For the most up-to-date information on the use of NSF Unidata software and data services, please consult the Software Documentation first.
>To: address@hidden >From: address@hidden (Melissa Petty) >Subject: NcVar::name() >Organization: . >Keywords: 199905281904.NAA15518 Hi Missy, > Hi there- > > I have been writing a test program using the netcdf c++ library, and > whenever I call NcVar::name(), I get a 'segmentation fault' error. > Here is the code: > > main() > > { > float *vplane; > NcDim *xdim, *ydim, *zdim; > int n; > NcVar *varPtr = 0; > float *varArray = 0; > int *varDims = 0; > > NcError* ncErr = new NcError(NcError::silent_nonfatal); > > NcFile nfile("ruc2.netcdf.19990428.i12.f00.nc", NcFile::ReadOnly); > if (!nfile.is_valid()) > { > cerr << "Unable to open netcdf file\n"; > exit(-1); > } > > cout << "num vars: " << nfile.num_vars() << "\n"; > > varPtr = nfile.get_var("v_hybr"); > if (!varPtr) > { > cerr << "Unable to obtain variable v_hybr" << "\n"; > exit(-1); > } > > cout << "num dims of var: " << varPtr->num_dims() << "\n"; > // cout << varPtr->name() << "\n"; > > xdim = nfile.get_dim("x"); > ydim = nfile.get_dim("y"); > zdim = nfile.get_dim("level"); > > cout << "x dim is " << xdim->size() << "\n"; > cout << "x dim name is " << xdim->name() << "\n"; > cout << "y dim is " << ydim->size() << "\n"; > // cout << "level is " << zdim->size() << "\n"; > > } > > With the varPtr->name() line commented out, everything is fine: > > num vars: 42 > num dims of var: 4 > x dim is 151 > x dim name is x > y dim is 113 > > > But when I uncomment it, I get this: > > num vars: 42 > num dims of var: 4 > Segmentation fault > > > Do you know what might be wrong? I am on a Linux machine. I've tried to duplicate the problem here, but the program worked fine, even with the cout << varPtr->name() << "\n"; statement included. Here's what I did, in case that helps explain the difference: On our Linux 2.2.6/i686 platform, I built netCDF version 3.4 from <ftp://ftp.unidata.ucar.edu/pub/netcdf/netcdf-3.4.tar.Z> using the following environment variable settings before running the configure script: CPPFLAGS="-DNDEBUG -Df2cFortran" CFLAGS=-O FC=/usr/bin/fort77 FFLAGS="-O -Nx400 -w" (though the Fortran flags should have nothing to do with the C++ library, and you should be able to leave out the Fortran interface if you're not using it, by using FC=""). Then I checked that "make all test" built the library and passed all the tests, including the C++ test. Just in case it makes any difference, the compiler versions were: $ cc --version 2.7.2.3 $ c++ --version egcs-2.90.29 980515 (egcs-1.0.3 release) At the top your code, I added the statement #include "netcdf.hh" to get all the prototypes for the netCDF functions defined. I compiled and linked from within the netcdf/cxx/ directory where I built the code instead of bothering to install it, using: $ c++ -c -I../libsrc -I. mp.cc $ c++ -o mp -I../libsrc -O mp.o libnetcdf_c++.a -L../libsrc -lnetcdf Then when I ran it, I got the following output: $ ./mp num vars: 111 num dims of var: 4 v_hybr x dim is 151 x dim name is x y dim is 113 This was on our version of a ruc2 netCDF file, which may not be the same as what you are using, but I doubt that the data has anything to do with this problem. If you have a similar platform and compilers, but still see the problem, maybe it is related to the data, in which case I'll need to get access to that file to debug the problem. --Russ _____________________________________________________________________ Russ Rew UCAR Unidata Program address@hidden http://www.unidata.ucar.edu