【发布时间】:2018-09-27 16:49:08
【问题描述】:
在 ChatClient.exe 中的 0x0F4CD6F0 (ucrtbased.dll) 处引发异常:0xC0000005:访问冲突读取位置 0x00000068。
几天来我一直在努力寻找这个错误的根源,我终于隔离了一个 sn-p 来说明我遇到的问题。在 switch 语句之后立即抛出异常。我不知道是什么导致了这段相对平凡的代码中的“访问冲突”,您可以在下面看到:
#include <iostream>
#include <string>
#include <conio.h>
int main(){
bool room = true, type = true;
string input;
unsigned int scroll = 0;
while (room) {
cout << input;
/* Input */
int ch = _getch();
switch (ch) {
case 72: /* scroll up */
if (!type && scroll != sizeof(unsigned int))
scroll++;
break;
case 80: /* scroll down */
if (!type && scroll != 0)
scroll--;
break;
case 13: /* Send message */
input.clear();
scroll = 0;
break;
case 27: // Quit loop
room = false;
break;
case 9: // Switch between scrolling and typing modes
if (type)
type = false;
else
type = true;
break;
default:
if (type && ch != -32) {
input.append((char*)ch);
}
break;
}
} <- Exception thrown, probably when the while loop condition is re-evaluated?
return 0;
}
将 Visual Studio 2017 与默认 IDE 调试工具结合使用。
【问题讨论】:
-
“我这几天一直在努力寻找这个错误的根源” 是否有任何困难包括使用调试器?
input.append((char*)ch);...什么?为什么要转换为指针?然后string将尝试读取该内存地址...您想要的是在相应的内存地址处附加一个ASCIIchar,而不是char*。因此,我投票决定关闭它,因为它是由一个简单的印刷错误引起的。 -
请避免使用magic numbers。如果通过例如
13你的意思是 ASCII 回车字符'\r'然后 say 所以使用实际字符。 -
我使用了“幻数”,因为 _getch 返回类型为 int。以后我会更加小心的。
-
input.append((char*)ch);是个坏主意 ;) -
没关系。使用 ASCII 然后
'\r' == 13.
标签: c++ access-violation