Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect documentation of nc_get_att_string #171

Closed
ckhroulev opened this issue Dec 7, 2015 · 3 comments
Closed

Incorrect documentation of nc_get_att_string #171

ckhroulev opened this issue Dec 7, 2015 · 3 comments

Comments

@ckhroulev
Copy link
Contributor

The documentation of nc_get_att_string should state that this function allocates storage for the value of the requested attribute and that the user is expected to call nc_free_string to free this storage.

@WardF
Copy link
Member

WardF commented Dec 8, 2015

Thanks for pointing this out; you are correct! I'll update the documentation shortly and then close out this ticket.

@ckhroulev
Copy link
Contributor Author

@WardF : Thanks! I also think that it's worth pointing out that when reading variable-length string attributes the user still needs to manually allocate (and free!) storage for the array of pointers passed to nc_get_att_string (string_attr below).

My minimal nc_get_att_string usage example looks like this:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <netcdf.h>

void check(int stat) {
  if (stat != NC_NOERR) {
    printf("NetCDF error: %s\n", nc_strerror(stat));
    exit(1);
  }
}

int main(int argc, char ** argv) {
  int stat = 0;

  int ncid = 0;
  stat = nc_open("test.nc", NC_NOWRITE, &ncid); check(stat);

  int varid = 0;
  stat = nc_inq_varid(ncid, "variable", &varid); check(stat);

  size_t attlen = 0;
  stat = nc_inq_attlen(ncid, varid, "attribute", &attlen); check(stat);

  char **string_attr = (char**)malloc(attlen * sizeof(char*));
  memset(string_attr, 0, attlen * sizeof(char*));

  stat = nc_get_att_string(ncid, varid, "attribute", string_attr); check(stat);

  for (size_t k = 0; k < attlen; ++k) {
    printf("variable:attribute[%d] = %s\n", k, string_attr[k]);
  }

  stat = nc_free_string(attlen, string_attr); check(stat);

  free(string_attr);

  stat = nc_close(ncid); check(stat);

  return 0;
}

@WardF
Copy link
Member

WardF commented Dec 8, 2015

I've added the documentation and your example to the code, thank you very much. I'll be committing and eventually merging it into master before the full 4.4.0 release. The documentation won't show up on our webpage until then, unfortunately. Thanks again for calling this out!

WardF added a commit that referenced this issue Dec 8, 2015
@WardF WardF closed this as completed Dec 9, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants