Re: [netcdfgroup] files with compound types not portable?

  • To: Ed Hartnett <ed@xxxxxxxxxxxxxxxx>
  • Subject: Re: [netcdfgroup] files with compound types not portable?
  • From: Jeff Whitaker <jswhit@xxxxxxxxxxx>
  • Date: Wed, 29 Apr 2009 13:32:02 -0600
Ed Hartnett wrote:
Howdy Jeff,

Just an update: we have confirmed this bug and I am working on it
now. I have also added this to our automatic testing, so that we can
see it break here on our test machines, and ensure that once it's
fixed, it doesn't break again.

I will let you know when this fix is available in the snapshot -
hopefully not more than a few days from now...

Thanks again for find this!

Ed
Ed: I'm not sure if this is the part of the same bug, or a different one. Attached is a C program that generates a compound variable from a struct containing a 16 bit integer and a 64 bit integer. If I use the gcc __packed__ attribute to turn off the padding for memory alignment, I get this from an ncdump of the resulting file:

data:

phony_var = {20000, 844424930431968} ;

whereas the correct result

data:

phony_var = {20000, 30000} ;

is obtained when the __packed__ attribute is not set and padding is added for memory alignment. However, as I noted in my original report, the file is not readable on platforms with a different default memory alignment.

This probably is the same bug, but if so this example shows how to trigger it without having access to multiple platforms.

-Jeff

--
Jeffrey S. Whitaker         Phone  : (303)497-6313
Meteorologist               FAX    : (303)497-6449
NOAA/OAR/PSD  R/PSD1        Email  : Jeffrey.S.Whitaker@xxxxxxxx
325 Broadway                Office : Skaggs Research Cntr 1D-113
Boulder, CO, USA 80303-3328 Web    : http://tinyurl.com/5telg

#include <stdlib.h>
#include <stdio.h>
#include "netcdf.h"

#define ERRCODE 2
#define ERR(e) {printf("Error: %s\n", nc_strerror(e)); exit(ERRCODE);}

int
main()
{
      int ncid, typeid, varid, dimid, retval;
      char name[NC_MAX_NAME + 1];
      int dimids[] = {0};

      struct s1 
      {
            short i;
            long long j;
      /*};*/
      } __attribute__ ((__packed__));

      struct s1 data[1];

      /* Create some phony data. */   
      data[0].i = 20000;
      data[0].j = 300000;

      /* Create a file with a compound type. Write a little data. */
      if ((retval = nc_create("test.nc", NC_NETCDF4, &ncid))) ERR(retval);;
      printf("size of compound %d\n",sizeof(struct s1));
      if ((retval = nc_def_compound(ncid, sizeof(struct s1), "cmp1", &typeid))) 
ERR(retval);;
      printf("offset i %d\n",NC_COMPOUND_OFFSET(struct s1,i));
      if ((retval = nc_insert_compound(ncid, typeid, "i", 
                             NC_COMPOUND_OFFSET(struct s1, i), NC_SHORT))) 
ERR(retval);;
      printf("offset j %d\n",NC_COMPOUND_OFFSET(struct s1,j));
      if ((retval = nc_insert_compound(ncid, typeid, "j", 
                             NC_COMPOUND_OFFSET(struct s1, j), NC_INT64))) 
ERR(retval);;
      if ((retval = nc_def_dim(ncid, "phony_dim", 1, &dimid))) ERR(retval);
      if ((retval = nc_def_var(ncid, "phony_var", typeid, 1, dimids, &varid))) 
ERR(retval);
      nc_put_var(ncid, varid, data);
      nc_close(ncid);
}
  • 2009 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the netcdfgroup archives: