title: Stack smashing detected during fread on binary file in C
body: <p>UPDATE IN BOTTOM====</p>
  
  <p>So a while ago I made the following function, which I successfully used to get the grey values from images (<em>w x h</em> dimension) that were converted to .bin-files. It just gives an array of all pixel values.
  <em>It was, however, not as a function like this but put in the main() immediately.</em></p>
  
  <pre><code>// read the BIN-file as grayscale image
  void decodeBIN(const char* filename, short image[], int w, int h){
      int i = 0;
      unsigned char buffer[16]; // no specific size attributed
      FILE *ptr;
  
      ptr = fopen(filename, "rb");
      if (!ptr){
          printf("\nUnable to open file!\n"); // error
      }
  
      while (!feof(ptr)){
          fread(buffer,2,1,ptr); // read w bytes to buffer
          image[i] = buffer[1];
          //printf("%u ", image[i]); // DEBUG
          i++;
      }
      fclose(ptr);
      printf("\nBinary image read (npixels: %i).\n", i-1); // DEBUG
  }
  </code></pre>
  
  <p>I decided to expand the code, so I rewrote it to the previous function and put it in a separate file for functions and also made a header file. The extra file for functions and the header file work 100% so that's not the issue. Now, this code does not work anymore and I get a stack smashing error. Some variables called after this function have also jumped to another value, so I figured the problem was with the buffer (I didn't know about the correct size for the buffer, but it worked...). After some experimentation and testing, I came up with the following function. I replaced the buffer with a char array named image2 to simply try and test it:</p>
  
  <pre><code>void decodeBIN(const char* filename, short image[], int w, int h){
      int i = 0, res;
      char image2[];
      FILE *ptr;
  
      ptr = fopen(filename, "rb"); //"MySnap_20180327-2239-010.bin"
      if (!ptr){
          printf("\nUnable to open file!\n"); // error
      }
  
      res = fread(image2,1,w*h,ptr) // need to read w*h pixels
  
      while (i &lt; w*h){ // DEBUG
          printf("%i ", (int)image2[i]); // DEBUG
          i++;
      }
      printf("\nRead %u bytes\n", res); // DEBUG
      fclose(ptr);
      printf("Binary image read (npixels: %i).\n", i); // DEBUG
  }
  </code></pre>
  
  <p>I'm a bit lost in how it used to work and all of a sudden when I move the code from main() to a function it stops working, so any help is welcome!</p>
  
  <p>Thanks in advance.</p>
  
  <p>Disclaimer: I'm aiming to write this with the help of as few libraries as possible</p>
  
  <p>===== UPDATE:</p>
  
  <p>After the answer of @alainmerigot I got this code, which helped with getting the correct values:</p>
  
  <pre><code>void decodeBIN(const char* filename, unsigned char image[], int w, int h){
      int i = 0, res;
      FILE *ptr;
  
      res = fread(image,sizeof(char),w*h,ptr) // need to read w*h pixels
      fclose(ptr);
  }
  </code></pre>
  
  <p>The segmentation fault and jumped variables are still in place though, so here a more upper-level oversight of what I'm doing:</p>
  
  <pre><code>char filenamePathed["[path of file]/file.bin"];
  short img1[npixels]; // npixels = w*h
  printf("i_file: %i\n", i_file); // correct value
  decodeBIN(filenamePathed, img_curr, w, h); // decode bin
  printf("i_file: %i\n", i_file); // value jumped
  while (i &lt; npixels){
      img1[i] = (short)img_curr[i];
      i++;
  }
  </code></pre>
  
  <p>Perhaps it is good to know that I'm doing this iteratively for multiple files (time series)? I also need it to end up in a (short) format (or integer, but eventually needs to be memory-efficient and pixels have a range of 0-255 so int is a bit abundant imo).</p>