C++程序在Python脚本上的崩溃

2024-09-29 21:27:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图发送一个运行特定python脚本的命令:但是,每当程序到达执行行时,就会发生这种情况:

中的0x69bd1f16处出现未处理的异常游戏服务器.exe:0xC0000005:访问冲突读取位置0x46f520ca

程序停止共振并崩溃。以下是所讨论的方法:

void ScriptManager::runScript(std::string scriptName, std::string args[])
{
    std::string py = "python " + scriptName;
    std::cout << py << std::endl;
    for(int i = 0; i < args->length(); i++)
    {
        py += " " + args[i];
        std::cout << py << std::endl;
    }
    std::cout << py << std::endl;

    std::system(py.c_str());

}

这将调用上述函数:

void DBFactory::dbRegisterUser(std::string username, std::string password)
{
    ScriptManager script;
    std::string data[] = {username, password};

    script.runScript("Python.py", data);
}

据我所知,脚本没有运行。如果有帮助的话,我也可以发布脚本。你知道吗


Tags: py程序脚本stringusernamescriptargspassword
1条回答
网友
1楼 · 发布于 2024-09-29 21:27:49

这就是问题所在:

for (int i = 0; i < args->length(); i++)
{
    py += " " + args[i];
    std::cout << py << std::endl;
}

args->length()等价于args[0].length();也就是说,您将数组中第一个字符串的长度作为索引。经过两次迭代之后,您将访问数组的末尾。最佳解决方案是(所有示例均未经测试):

    使用^{}(C++ 11):< /p>

    void DBFactory::dbRegisterUser(std::string username, std::string password)
    {
        ScriptManager script;
    
        script.runScript("Python.py", {username, password});
    }
    
    void ScriptManager::runScript(std::string scriptName, std::array<std::string, 2> args)
    {
        std::string py = "python " + scriptName;
        std::cout << py << std::endl;
        for (std::string s : args)
        {
                py += " " + s;
                std::cout << py << std::endl;
        }
        std::cout << py << std::endl;
    
        std::system(py.c_str());
    }
    

    使用^{}(示例使用C++ 03):

    void DBFactory::dbRegisterUser(std::string username, std::string password)
    {
        ScriptManager script;
        int tmp[2] = {username, password};
    
        script.runScript("Python.py", std::vector<std::string>(&tmp[0], &tmp[0]+2));
    }
    
    void ScriptManager::runScript(std::string scriptName, std::vector<std::string> args)
    {
        std::string py = "python " + scriptName;
        std::cout << py << std::endl;
        for(std::vector<std::string>::iterator it = args.begin(); it != args.end(); it++)
        {
            py += " " + *it;
            std::cout << py << std::endl;
        }
        std::cout << py << std::endl;
    
        std::system(py.c_str());
        }
    
  1. 将数组大小作为参数传递:

    void DBFactory::dbRegisterUser(std::string username, std::string password)
    {
        ScriptManager script;
    
        script.runScript("Python.py", {username, password}, 2);
    }
    
    void ScriptManager::runScript(std::string scriptName, std::string args[], int size)
    {
        std::string py = "python " + scriptName;
        std::cout << py << std::endl;
        for(int i=0; i<size; i++)
        {
            py += " " + args[i];
            std::cout << py << std::endl;
        }
        std::cout << py << std::endl;
    
        std::system(py.c_str());
    }
    

我个人更喜欢例1,也会像避免瘟疫一样避免例3。示例2运行良好,但可能没有示例1快。你知道吗

相关问题 更多 >

    热门问题