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

Re: 20020228: problems with 2 large double arrays



>To: address@hidden
>From: Laura Scott <address@hidden>
>Subject: problems with 2 large double arrays
>Organization: UCAR/Unidata
>Keywords: 200202281919.g1SJJxx27621

Hi Laura,

> I need to create two arrays for a netCDF file, one is 32767x42 char and
> the other is 32767x255 char.  My initial requirements were for just the
> first array.  No matter what I tried to do to initialize that array - I
> would get several records of just memory garbage.
> 
> NOTE: I add a datafile name to this array each time a new file is
> entered into my archive system, so when I create the netCDF datafile,
> only one array is filled and the others I would like to be empty
> strings.  As each new file enters the archive - I get this array and
> look for the first "" entry, add my new filename into that position and
> put the data back into the netCDF file.
> 
> When my requirements changed and I had to add the second array - I get a
> segmentation fault when the varput command is called.  If I reduce the
> first dimension of the second array (say to 500) for the varput command
> - no seg fault.  I might add that I still have the vardef for 32767,255?
> 
> So basically - I was wondering how I can cleanly define these two large
> arrays, initialize them to "", and not get a seg fault when I write the
> netCDF datafile out?  I am using C 4.0 version on a UNIX box.

[Note added later: the answer given here is *wrong*.  See explanation
below]

I think the problem is that you are giving the address of a
2-dimensional array, assuming that is the same as the address of the
start of the values of the array in memory, but that's not the way
multidimensional arrays work in C.  For example, when you call

  status = ncvarput(my_ncid,dfileID,Dstart,Dcount,datafile);

you may think that the "datafile" argument is the address of the start
of the values of the array declared as

  char datafile[MAX_DATA][cforty2];

but it's actually only the address of the start of an array of
MAX_DATA pointers to the rows of the array.  What you really want is
probably

  status = ncvarput(my_ncid,dfileID,Dstart,Dcount,&datafile[0][0]);

instead, because datafile is not the same as &datafile[0][0] (which
you can see by printing them out).  The expression &datafile[0][0]
means the address of the [0][0] element of the the datafile array,
whereas "datafile" is the same as &datafile[0], which just means the
address of the start of the block of pointers allocated to point to
the rows of the array.

Similarly, instead of 

  status = ncvarput(my_ncid,md_fileID,Dstart,Dcount2,md_datafile);

you probably meant 

  status = ncvarput(my_ncid,md_fileID,Dstart,Dcount2,&md_datafile[0][0]);

Another way around this is to use one-dimensional arrays and do the
address calculations yourself, because for a one dimensional C array,
it is true that the array name is the same as the address of the first
element.

[Note about wrong answer above:  while it's true for dynamically
allocated multidimensional arrays that "datafile" is not the same as
"&datafile[0][0]", these actually are the same for statically
allocated multidimensional arrays, as datafile is declared to be in
the example above.]

--Russ

_____________________________________________________________________

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