I am facing a strange problem over a fairly simple piece of code. The relevant portion of the code is given below:
void foo(int32 in_sd_id, int32 out_sd_id)
{
int32 nsds; /* number of data sets in the file */
int32 nattr; /* number of global attributes in the file */
int32 attr_cnt; /* count of number of attribute */
int32 attr_index; /* attribute index */
int32 attr_type, attr_size; /* attribute type and size */
char attr_name[40];
ret = SDfileinfo(in_sd_id, &nsds, &nattr);
printf("nattr is %d\n", nattr);
/* test to see if num_datasets and num_global_attr can be retrieved from in_sd_id */
if (ret == -1)
{
fprintf(stdout, "cannot read information from input file \n");
exit(EXIT_FAILURE);
}
else
{
/* loop through each global attributes */
for (attr_index=0; attr_index<nattr; attr_index++)
{
printf("attr_index:nattr is %d:%d\n", attr_index, nattr);
/* test to see if the file or dataset do indeed contain attributes */
if (SDattrinfo(in_sd_id, attr_index, attr_name, &attr_type, &attr_cnt) == FAIL)
fprintf(stdout, "Cannot read information for attribute %d\n", attr_index);
else
{
DO SOMETHING
}
}
}
}
The problem is with the nattr
variable. Say for example nattr
should be 11
, within the for
loop when i print the value of nattr
, I get it as 11
for sometime but then suddenly it blows up to a huger number like 1869501279
. I am not doing anything else with this nattr
variable in the rest of the code. I have double and tripe checked that. So I am not sure why its blowing up suddenly. The debug statement from one sample run is given below:
nattr is 11
attr_index:nattr is 0:11
attr_index:nattr is 1:11
attr_index:nattr is 2:11
attr_index:nattr is 3:11
attr_index:nattr is 4:11
attr_index:nattr is 5:11
attr_index:nattr is 6:11
attr_index:nattr is 7:11
attr_index:nattr is 8:1869501279
attr_index:nattr is 9:1850957672
attr_index:nattr is 10:1850957672
attr_index:nattr is 11:1850957672
Cannot read information for attribute 11
attr_index:nattr is 12:1850957672
Cannot read information for attribute 12
attr_index:nattr is 13:1850957672
Any help as to what may be going on here. Thanks