title: Segfault in fread()
body: <p>I'm trying to read a BMP image (greyscales) with C, save values into an array, and convert this array to a string with values separated with a comma.</p>
  
  <p>My program worked well under Windows 7 64-bit, but I had to move to Windows XP 32-bit because of library compatibility problems.</p>
  
  <p>I have 1,750 images to read, and I want to store all of them in a single string.</p>
  
  <p>When I launch my program it goes fine until the 509:th image, then I get a Segmentation Fault caused by fread(). Here's my code: </p>
  
  <pre><code>int i=0,j,k,num,len,length,l;
  unsigned char *Buffer;
  FILE *fp;
  char *string,*finalstring;
  char *query;
  char tmp2[5],tmp[3];
  query = (char *)malloc(sizeof(char)*200000000);
  string = (char *)malloc(sizeof(char)*101376);
  Buffer = (unsigned char *)malloc(sizeof(unsigned char)*26368);
   BITMAPFILEHEADER bMapFileHeader;
  BITMAPINFOHEADER bMapInfoHeader;
  length = 0;
  
  
    for (k =1;k&lt;1751;k++)
      {
      strcpy(link,"imagepath");
      //here just indexing the images from 0000 to 1750
      sprintf(tmp2,"%.4d",k);
      strcat(link,tmp2);
      strcat(link,".bmp");
  
  
         fp = fopen(link, "rb");
         num = fread(&amp;bMapFileHeader,sizeof(BITMAPFILEHEADER),1,fp);
         num = fread(&amp;bMapInfoHeader,sizeof(BITMAPINFOHEADER),1,fp);
      //seek beginning of data in bitmap
       fseek(fp,54,SEEK_SET);
      //read in bitmap file to data
  
      fread(Buffer,26368,1,fp);
      l=0;
  
      for(i=1024;i&lt;26368;i++)
      {
        itoa(Buffer[i],tmp,10);
        len = strlen(tmp);
        memcpy(string+l,tmp,len);
        memcpy(string+l+len,",",1);
        l = l+len+1;
  
      }
  
      memcpy(query,"",1);
      memcpy(string,"",1);
      printf("%i\n",k);
  
      }
  </code></pre>
  
  <p>Thanks</p>