当我为十六进制数组中的缓冲区分配空间时,我的代码不断中断(即抛出访问冲突异常)。
我在 main 中将十六进制数组声明为两个星形指针,并通过引用传递它。
main.cpp 中的某处
char ** hexArray = nullptr;
fileio.cpp 中的某处
void TranslateFile(char * byteArray, char **& hexArray, int numberOfBytes, char buffer[])
{
int temp = 0;
//Convert bytes into hexadecimal
for(int i = 0; i < numberOfBytes; i++)
{
//Convert byteArray to decimal
atoi(&byteArray[i]);
//Set temp equal to byteArray
temp = byteArray[i];
//Convert temp to hexadecimal and store it in hex array
itoa(temp, buffer, 16);
//Allocate room for buffer
hexArray[i] = new char[strlen(buffer) + 1]; //CODE BREAKS HERE
//Copy buffer into newly allocated spot
strcpy(hexArray[i], buffer);
}
}