I'm using an Artifical Neural Network library made by a researcher for which i'm doing an internship. There's a function reading pgm images and loading them into arrays. The function works just fine at first. Here's the interesting part of it:
unsigned char *tmp=(unsigned char *)calloc(width, sizeof(unsigned char));
for(int i=0;i<height;i++)
{
int r=(int)fread(tmp, sizeof(unsigned char), width, fp);
if(r!=width)
{
fprintf(stderr, "Error while reading PGM file [%s] (%d bytes read instead of %d)\n", filename, r, width);
Del2D(height, pix);
pix=NULL;
free(tmp);
fclose(fp);
return(0);
}
for(int j=0;j<width;j++)
pix[i][j]=(unsigned char)tmp[j];
}
name=strdup(filename);
fclose(fp);
free(tmp);
I do get a segmentation fault at the line
int r=(int)fread(tmp,sizeof(unsigned char), width, fp);
However, i only get this error for one pgm image. The only reason i see for that is because it's the largest one in terms of height and width (900x900). I've looked a bit about the usage of fread
and it seems to me that this portion of code is fine. I've not seen any limitation about the number of characters readable at once by the function either.
Any idea?
Edit: The width parameter was equal to -1 for this particular image, but there's no reason for that since reading the width works just fine for every other image. I'm gonna guess that something's wrong with the image itself. The program works fine for 1024*1024 pgm images I found afterward so it's not a size issue. Thanks for your answers.