title: Why does this C++ code crash?
body: <p>I'm trying to run this code in Windows 7, after it has compiled successfully. Any ideas on why it crashes? I've given it a random generated binary file as an input to test, but as soon as it begins, the application crashes.</p>
  
  <pre><code>#include &lt;cstdio&gt;
  #include &lt;cstdint&gt;
  using namespace std;
  
  long getFileSize(FILE *file) {
          long currentPosition, endPosition;
          currentPosition = ftell(file);
          fseek(file, 0, 2);
          endPosition = ftell(file);
          fseek(file, currentPosition, 0);
          return endPosition;
  }
  
  struct frame {
          uint8_t bytes[535];
  };
  
  struct spectralFrame {
          uint8_t bytes[512];
  };
  
  struct spectralFrameWord {
          uint16_t words[256];
  };
  
  int main() {
          char *filePath = "C:\\Payload\\Untitled.bin";
          uint8_t *fileBuffer;
          FILE *file = NULL;
          file = fopen(filePath, "rb");
  /*      if((file = fopen(filePath, "rb")) == NULL)
                  cout &lt;&lt; "Failure." &lt;&lt; endl;
          else
                  cout &lt;&lt; "Success." &lt;&lt; endl;
  */
          long fileSize = getFileSize(file);
          fileBuffer = new uint8_t[fileSize];
          fread(fileBuffer, fileSize, 1, file);
          fclose (file);
          long frameCount = fileSize / 535;
          frame *frames = new frame[frameCount];
          for(int i = 0, j = 0, k = 0; i &lt; fileSize; i++) {
                  frames[j].bytes[k++] = fileBuffer[i];
                  if((i % 534) == 0) {
                          j++;
                          k = 0;
                  }
          }
          delete fileBuffer;
          fileBuffer = NULL;
          spectralFrame *spectralFrames = new spectralFrame[frameCount];
          for(int i = 0; i &lt; frameCount; i++) {
                  for(int j = 22, k = 0; j &lt; 534; j++) {
                          spectralFrames[i].bytes[k++] = frames[i].bytes[j];
                  }
          }
          delete frames;
          frames = NULL;
          spectralFrameWord *spectralFrameWords = new spectralFrameWord[frameCount];
          uint16_t word;
          for(int i = 0; i &lt; frameCount; i++) {
                  for(int j = 0, k = 0; i &lt; 511;) {
                          word = spectralFrames[i].bytes[j++] &lt;&lt; 8;
                          spectralFrameWords[i].words[k++] = word | spectralFrames[i].bytes[j++];
                  }
          }
          delete spectralFrames;
          spectralFrames = NULL;
          filePath = "C:\\Payload\\Untitled.tsv";
          file = fopen(filePath, "w");
  /*      if((file = fopen(filePath, "w")) == NULL)
                  cout &lt;&lt; "Failure." &lt;&lt; endl;
          else
                  cout &lt;&lt; "Success." &lt;&lt; endl;
  */
          for(int i = 0; i &lt; 256; i++) {
                  fprintf (file, "%u\t%u\n", i, spectralFrameWords[0].words[i]);
          }
          fclose (file);
          return 0;
  }
  </code></pre>