"Exception thrown: write access violation. **_Left** was 0xCCCCCCCC." 为什么我的函数只能使用某些值正确调用?



我正在尝试编写一个数字转换程序,该程序将采用十进制、十六进制或二进制数字,并将其转换为不同的类型。此程序将为原始数字类型和要转换到的类型分配枚举值,并将它们分配给int变量。这些变量与要转换的数字一起在函数调用中传递。

到目前为止,我的代码如下:


#include<iostream>
#include<string>

using namespace std;
void convert(int numType, int conversionType, string userNum);



int main()
{
cout << "What type of number would you like to provide?n";
cout << "Decimal(1), Binary(2), Hexadecimal(3)n";

int numType;

cin >> numType;

if (numType < 1 || numType > 3)
{
cout << "Please enter an appropriate value.n";
cin >> numType;
}

string userNum;

cout << "Please enter the number.n";

cin >> userNum;

int conversionType;
cout << "What type of number would you like to convert to?n";
if (numType == 1)
{
cout << "Binary(2) or Hexadecimal(3)n";
cin >> conversionType;
if (conversionType < 2 || conversionType > 3)
{
cout << "Please enter an appropriate value.n";
cin >> conversionType;
}
}
else if (numType == 2)
{
cout << "Decimal(1) or Hexadecimal(3)n";
cin >> conversionType;
if (conversionType < 1 || conversionType == 2 || conversionType > 3)
{
cout << "Please enter an appropriate value.n";
cin >> conversionType;
}
}
else if (numType == 3)
{
cout << "Decimal(1) or Binary(2)n";
cin >> conversionType;
if (conversionType < 1 ||conversionType >2)
{
cout << "Please enter an appropriate value.n";
cin >> conversionType;
}
}
cout << "The conversion yeilds: ";
convert(numType, conversionType, userNum);

return 0;
}

void convert(int numType, int conversionType, string userNum)
{
int num = stoi(userNum);

if (numType == 1 && conversionType == 2)
{
int binNum[64];
int i;
for (i = 0; num > 0; i++)
{
binNum[i] = (num % 2);
num /= 2;
}

for (i -= 1; i >= 0; i--)
{
cout << binNum[i];
}
cout << endl;
}

if (numType == 1 && conversionType == 3)
{
string hexNum[64];
int i = 0;

while (num != 0)
{
int tempVal;

tempVal = (num % 16);

if (tempVal < 10)
{
num += 48;
}
else
{
num += 55;
}

hexNum[i] = num;

i++;
num /= 16;
}
for (i -= 1; i > 0; i--)
{
cout << hexNum[i];
}
cout << endl;
}
}

当我使用numType=1和conversionType=2(十进制到二进制(进行测试时,一切都很好。然而,当我使用numType=1和conversionType=3时,我会得到这个错误。

引发未处理的异常:写入访问冲突。


**_Left** was 0xCCCCCCCC. occurred

在xstring文件中,我看到了这个:

static _CONSTEXPR17 void assign(_Elem& _Left, const _Elem& _Right) noexcept {
_Left = _Right;
}

有人能帮我理解为什么当conversionType为3时会抛出这个错误,而当它为2时却不会?我已经进行了测试,以确保conversionType在函数调用之前确实存储了值。

编辑:删除所有不相关代码的建议非常有用,因为我发现错误并不是我所认为的函数调用。它在十六进制的转换公式中。正如Igor所指出的,我把公式搞砸了,当我进行修复时,就不再抛出异常。老实说,我想不起为什么我认为错误发生在函数调用中,但我毕竟是新手。感谢大家花时间来看我的问题并发表评论。

编辑:在我写答案的时候,你似乎发现了。

答案底部的解释。你应该替换这个:

if (numType == 1 && conversionType == 3)
{
string hexNum[64];
int i = 0;
while (num != 0)
{
int tempVal;
tempVal = (num % 16);
if (tempVal < 10)
{
num += 48;
}
else
{
num += 55;
}
hexNum[i] = num;
i++;
num /= 16;
}
for (i -= 1; i > 0; i--)
{
cout << hexNum[i];
}
cout << endl;
}

到此:

if (numType == 1 && conversionType == 3)
{
string hexNum[64];
int i = 0;
while (num != 0)
{
int tempVal;
tempVal = (num % 16);
if (tempVal < 10)
{
tempVal += '0'; // tempVal instead num and not use magic constant
}
else
{
tempVal += 'A' - 10; // tempVal instead num and not use magic constant
}
hexNum[i] = tempVal; // tempVal instead num
i++;
num /= 16;
}
for (i -= 1; i >= 0; i--) // >= instead > to not cut last hex digit
{
cout << hexNum[i];
}
cout << endl;
}

解释:您使用num而不是tempVal,所以无论如何,num至少增加了16(在一种情况下为48,在另一种情况为55(,所以当您将num除以16时,它永远不会变为,所以i增加,直到haxNum[i]从堆栈中出来,0xCCCCCCCC被抛出。

Igor Tandetnik在评论中也做出了同样的解释。

@Alan Birtles说得对,这是的堆栈访问问题

最新更新