-
Notifications
You must be signed in to change notification settings - Fork 263
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
Comments
Thanks for pointing this out; you are correct! I'll update the documentation shortly and then close out this ticket. |
@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 My minimal #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;
} |
I've added the documentation and your example to the code, thank you very much. I'll be committing and eventually merging it into |
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: