I get the following error from the code I wrote - Runtime Check Error # 2 - Zeroing the stack around the variable "pChar"
From the research, it suggests that the problem is related to pHexValueBuffer = new char [256], and memset, and how I use true, to store values ββfor returning a hexadecimal number instead of a decimal. My research shows that somehow I go beyond the memory that I installed, just not understanding how to do it.
Any suggestions for fixing the problem?
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; }
source share