I am new to C++ and dynamic memory allocation.
I have this code to convert a number from decimal to hexadecimal, that uses a dynamic array:
int hexLen = value.length();
char* arrayPtr = new char[hexLen];
_itoa_s(stoi(dec), arrayPtr, 16);
string hexVal = static_cast<string>(arrayPtr);
delete[] charArrayptr;
When I used an array with a fixed size, _itoa_s()
worked with it. However, when using a dynamic array, the compiler says that a method with the arguments given doesn't exist.
Is this something that I did wrong, or will _itoa_s()
simply not work with a dynamic array?
Version with non-dynamic array (that works):
const int LENGTH = 20;
char hexCharArray[LENGTH];
_itoa_s(stoi(dec), hexCharArray, 16);