我做了一个简单的程序,我在其中执行了一个带有用户输入的参数的程序,但是每当我运行它时,它都会给我错误
"Unhandled exception at 0x00f138b7 in SZP_Client.exe: 0xC0000005: Access violation writing location 0x0000000a."
我的代码是
char* command_text;
cout << "Enter SERVER IP Address without Port ";
cin >> command_text;
char* command_str = strdup("PServLib.exe \"127.0.0.1\" \"30760\" ");
string command_string_out_final= command_str;
command_string_out_final.append(command_text);
command_string_out_final = command_string_out_final + " \"3627\"";
system(command_string_out_final.c_str());
为什么我的代码会引发访问冲突,我该如何解决?
最佳答案
罪魁祸首是 cin >> command_text
。当写入 char*
时,它假定这是一个足够大的 char 数组来保存正在写入的值。相反,它是一个未初始化的 char*
,因此您正在写入任意内存。
最简单的修复方法是将 command_text
更改为 std::string
。
关于c++ - 访问冲突写入位置 0x0000000a,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15123799/