You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
@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>voidcheck(int stat) {
if (stat != NC_NOERR) {
printf("NetCDF error: %s\n", nc_strerror(stat));
exit(1);
}
}
intmain(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);
return0;
}
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!
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 callnc_free_string
to free this storage.The text was updated successfully, but these errors were encountered: