title: C++ Stack around a variable is corrupted
body: <p>I'm getting the following error from the code I've written - 
  Run-Time Check Failure #2 - Stack around the variable 'pChar' was corrupted</p>
  
  <p>From research it is suggestive that the problem has to do with pHexValueBuffer = new char[256] and the memset and how I'm using the veritable - to store values to return a Hex number instead of Decimal. My research suggests that somehow I'm going out of bounds with the memory I've set, just not understanding the how.</p>
  
  <p>Any suggestions on how to fix the issue?</p>
  
  <pre><code>void DecToHex()
  {
      string input;
      int total = 0;
      int index = 254;
      char pChar;
      char* pHexValueBuffer = new char[256];
      memset(pHexValueBuffer, 0, 256 );
      pHexValueBuffer[255] = '\0';
  
      cout &lt;&lt; "Enter a Decimal Number\n\n" &lt;&lt; flush;
  
      cin &gt;&gt; input;
      unsigned int iValue = atoi(input.c_str());
  
  
      do
      {
          --index;
          unsigned int iMod = iValue % 16;
          if( iMod &gt; 9 )
          {
              switch (iMod)
              {
              case 10:
                  pHexValueBuffer[index] = 'A';
                  break;
              case 11:
                  pHexValueBuffer[index] = 'B';
                  break;
              case 12:
                  pHexValueBuffer[index] = 'C';
                  break;
              case 13:
                  pHexValueBuffer[index] = 'D';
                  break;
              case 14:
                  pHexValueBuffer[index] = 'E';
                  break;
              case 15:
                  pHexValueBuffer[index] = 'F';
                  break;
              default:
                  break;
              }
  
          }
          else
          {
              itoa(iMod, &amp;pChar, 10 );
              pHexValueBuffer[index] = pChar;
          }
          iValue = iValue/16;
  
      } while( iValue &gt; 0 );
  
      cout &lt;&lt; "Decimal Number = " &lt;&lt; &amp;pHexValueBuffer[index];
      delete []pHexValueBuffer;
      pHexValueBuffer = NULL;
  }
  </code></pre>