title: Heap Corruption in release mode only
body: <p>This is a printing thread that prints the statistic of my currently running program</p>
  
  <pre><code>void StatThread::PrintStat(){
  clock_t now = 0;
  UINT64 oneMega = 1&lt;&lt;20;
  const char* CUnique = 0;;
  const char* CInserted = 0;;
  while((BytesInserted&lt;=fileSize.QuadPart)&amp;&amp;flag){
      Sleep(1000);
      now = clock();
      CUnique = FormatNumber(nUnique);
      CInserted = FormatNumber(nInserted);
      printf("[ %.2f%%] %u / %u dup %.2f%% @ %.2fM/s %.2fMB/s %3.2f%% %uMB\n",
          (double)BytesInserted*100/(fileSize.QuadPart),
          nUnique,nInserted,(nInserted-nUnique)*100/(double)nInserted,
          ((double)nInserted/1000000)/((now - start)/(double)CLOCKS_PER_SEC),
          ((double)BytesInserted/oneMega)/((now - start)/(double)CLOCKS_PER_SEC),
          cpu.GetCpuUtilization(NULL),cpu.GetProcessRAMUsage (true));
      if(BytesInserted==fileSize.QuadPart)
          flag=false;
  }
  delete[] CUnique;    //would have worked with memory leak if commented out
  delete[] CInserted;  // crash at here! heap corruption 
  }
  </code></pre>
  
  <p>This is FormatNumber that returns a pointer to a char array</p>
  
  <pre><code>const char* StatThread::FormatNumber(const UINT64&amp; number) const{
  char* result = new char[100];
  result[0]='\0';
  _i64toa_s(number,result,100,10);
  DWORD nDigits = ceil(log10((double)number));
  result[nDigits] = '\0';
  if(nDigits&gt;3){
      DWORD nComma=0;
      if(nDigits%3==0)
          nComma = (nDigits/3) -1;
      else
          nComma = nDigits/3;
      char* newResult = new char[nComma+nDigits+1];
      newResult[nComma+nDigits]='\0';
      for(DWORD i=1;i&lt;=nComma+1;i++){
          memcpy(newResult+strlen(newResult)-i*3-(i-1),result+strlen(result)-i*3,3);
          if(i!=nComma+1){
              *(newResult+strlen(newResult)-4*i) = ',';   
          }
      }
      delete[] result; 
      return newResult;
  }
  return result;
  }
  </code></pre>
  
  <p>What is really weird was that it crashed only in release mode because of a heap corruption, but run smoothly in debug mode. I've already checked everywhere and found no obvious memory leaks, and even Memory Leak Detector said so too.</p>
  
  <pre><code>Visual Leak Detector Version 2.2.3 installed.
  The thread 0x958 has exited with code 0 (0x0).
  No memory leaks detected.
  Visual Leak Detector is now exiting.
  The program '[5232] Caching.exe' has exited with code 0 (0x0).
  </code></pre>
  
  <p>However, when run in release mode,it threw an error that said my program stop working and I clicked on debug, it pointed to the line that caused the heap corruption. </p>
  
  <pre><code>The thread 0xe4c has exited with code 0 (0x0).
  Unhandled exception at 0x00000000770E6AE2 (ntdll.dll) in Caching.exe:          0xC0000374: A heap has been corrupted (parameters: 0x000000007715D430).
  </code></pre>
  
  <p>If I commented out this line, it worked fine but Memory Leak Detector would have complained about memory leak! I don't understand how to cause a heap corruption when there was no memory leaks, (at least that's what the Leak Detector said). Please help, Thank you in advance.</p>
  
  <p>Edit:
  Heap corruption was fixed, because in the very last iteration, I still copied 3 byes to the front instead of whatever is leftover. Thank you all for helps!</p>
  
  <pre><code>const char* StatThread::FormatNumber(const UINT64&amp; number) const{
  char* result = new char[100];
  result[0]='\0';
  _ui64toa_s(number,result,100,10);
  DWORD nDigits = (DWORD)ceil(log10((double)number));
  if(number%10==0){
      nDigits++;
  }
  result[nDigits] = '\0';
  if(nDigits&gt;3){
      DWORD nComma=0;
      if(nDigits%3==0)
          nComma = (nDigits/3) -1;
      else
          nComma = nDigits/3;
      char* newResult = new char[nComma+nDigits+1];
      DWORD lenNewResult = nComma+nDigits;
      DWORD lenResult = nDigits;
      for(DWORD i=1;i&lt;=nComma+1;i++){
          if(i!=nComma+1){
              memcpy(newResult+lenNewResult-4*i+1,result+lenResult-3*i,3);
              *(newResult+lenNewResult-4*i) = ',';    
          }
          else{
              memcpy(newResult,result,lenNewResult-4*(i-1));
          }
      }
      newResult[nComma+nDigits] = '\0';
      delete[] result; 
      return newResult;
  }
  return result;
  }
  </code></pre>