我从编写的代码中收到以下错误 - 运行时检查失败 #2 - 变量“pChar”周围的堆栈已损坏
从研究表明,问题与 pHexValueBuffer = new char[256] 和 memset 以及我如何使用名副其实的 - 存储值以返回十六进制数而不是十进制数有关。我的研究表明,不知何故,我已经超出了我设定的记忆的界限,只是不了解如何。
有关如何解决此问题的任何建议?
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 << "Enter a Decimal Number\n\n" << flush;
cin >> input;
unsigned int iValue = atoi(input.c_str());
do
{
--index;
unsigned int iMod = iValue % 16;
if( iMod > 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, &pChar, 10 );
pHexValueBuffer[index] = pChar;
}
iValue = iValue/16;
} while( iValue > 0 );
cout << "Decimal Number = " << &pHexValueBuffer[index];
delete []pHexValueBuffer;
pHexValueBuffer = NULL;
}