我做了一个简单的程序,我在其中执行一个带有用户输入参数的程序,但每当我运行它时,它都会给我错误
"Unhandled exception at 0x00f138b7 in SZP_Client.exe: 0xC0000005: Access violation writing location 0x0000000a."
Run Code Online (Sandbox Code Playgroud)
我的代码是
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());
Run Code Online (Sandbox Code Playgroud)
为什么我的代码会引发访问冲突,我该如何解决?
罪魁祸首是cin >> command_text.写入时char*,假设这是一个足够大的char数组,用于保存正在写入的值.相反,它是一个未初始化的char*,所以你写入任意记忆.
最简单的解决方法是改变command_text成std::string.
| 归档时间: |
|
| 查看次数: |
1049 次 |
| 最近记录: |