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

[netCDF #PXO-118189]: Segmentation fault C++



The attached C program does work. There might be a type issue
since he data in simpl_xy.nc is int, while you are trying to read floats.
That is why I used nc_get_var_float.


> That was one of the first things I tried... But when I do this, I get 
> "netCDF: Start+Count exceeds dimension bound". Is it working on your PC?
> 
> When I create that 2D array with nx and ny-2 instead of ny, then there are no 
> missing numbers in between, but the last 2 numbers are missing. It is just so 
> strange.. :/
> ________________________________________
> Von: Unidata netCDF Support [address@hidden]
> Gesendet: Samstag, 16. Dezember 2017 23:48
> An: Mittelstädt, Lisa
> Cc: address@hidden
> Betreff: [netCDF #PXO-118189]: Segmentation fault C++
> 
> Ok, I can see at least one major problem. When you read a 2-D variable,
> it is read as a 1-D variable of size dimX*dimY. So, you need to declare
> data in as float* not float** and you need to do a single datain = new 
> float[nx*ny].
> 
> 
> > I am using netcdf-cxx-4.2.
> > When I try to read the data with allocated arrays, then the data is not 
> > read completely (in simple_xy.nc there are numbers from 0 to 71 and every 
> > 11 numbers 4 numbers are missing in dataIn). In the internet people say 
> > that one should use vectors instead of this allocating thing, but I guess 
> > it is not working with the get() function?
> >
> > In this case I get the "double free or corruption error". I have also these 
> > programs with the "normal" arrays and segmentation fault. I can send them 
> > to you, too, if I really can't use dynamic arrays.
> > ________________________________________
> > Von: Unidata netCDF Support [address@hidden]
> > Gesendet: Mittwoch, 13. Dezember 2017 22:48
> > An: Mittelstädt, Lisa
> > Cc: address@hidden
> > Betreff: [netCDF #PXO-118189]: Segmentation fault C++
> >
> > A couple of questions.
> > 1. Are you using netcdf-3 or netcdf4?
> > 2. Can you post your program here as an email attachment?
> >
> > >
> > > I am using netCDF in C++. With relative small arrays everything works
> > > fine.  But when I try to use an 4D array with 7*10^6 floats (it is also
> > > failing with a 1D array), I get segmentation fault error in that line,
> > > where I declared my array. I would like to allocate the array in the
> > > heap, but the function for writing the netcdf data in the array (e.g.,
> > > data->get(&dataIn[0][0], NX, NY) as in simple_xy_rd.cpp) is only working
> > > with "normal" arrays. Is there any way to do this with allocated arrays?
> > >
> > > Best regards,
> > > Lisa Mittelstaedt
> > >
> > >
> >
> > =Dennis Heimbigner
> > Unidata
> >
> >
> > Ticket Details
> > ===================
> > Ticket ID: PXO-118189
> > Department: Support netCDF
> > Priority: Normal
> > Status: Open
> > ===================
> > NOTE: All email exchanges with Unidata User Support are recorded in the 
> > Unidata inquiry tracking system and then made publicly available through 
> > the web.  If you do not want to have your interactions made available in 
> > this way, you must let us know in each email you send to us.
> >
> >
> >
> >
> 
> =Dennis Heimbigner
> Unidata
> 
> 
> Ticket Details
> ===================
> Ticket ID: PXO-118189
> Department: Support netCDF
> Priority: Normal
> Status: Open
> ===================
> NOTE: All email exchanges with Unidata User Support are recorded in the 
> Unidata inquiry tracking system and then made publicly available through the 
> web.  If you do not want to have your interactions made available in this 
> way, you must let us know in each email you send to us.
> 
> 
> 
> 

=Dennis Heimbigner
  Unidata


Ticket Details
===================
Ticket ID: PXO-118189
Department: Support netCDF
Priority: Normal
Status: Open
===================
NOTE: All email exchanges with Unidata User Support are recorded in the Unidata 
inquiry tracking system and then made publicly available through the web.  If 
you do not want to have your interactions made available in this way, you must 
let us know in each email you send to us.
#include <stdlib.h>
#include <stdio.h>
#include <netcdf.h>

int
main()
{       
    int stat;
    int ncid, varid;
    int dimids[2];
    size_t dimX, dimY, product, i;
    float* datain;
    
    if((stat = nc_open("simple_xy.nc",0,&ncid))) abort();
    if((stat = nc_inq_varid(ncid,"data",&varid))) abort();
    if((stat = nc_inq_vardimid(ncid,varid,dimids))) abort();
    if((stat = nc_inq_dimlen(ncid,dimids[0],&dimX))) abort();
    if((stat = nc_inq_dimlen(ncid,dimids[1],&dimY))) abort();
 
    product = dimX * dimY;

    datain = calloc(sizeof(float)*product,1);
    if(datain == NULL) abort();
    
    if((stat = nc_get_var_float(ncid,varid,datain))) abort();
        
    for(i=0;i<product;i++) {
        printf("[%lu] %g\n",(unsigned long)i,datain[i]);
    }
    fflush(stdout);
    return 0;    
}