我正在做一个学校作业,我必须调用execvp,其方法签名如下
execvp(const char* file, char* const argv[])
Run Code Online (Sandbox Code Playgroud)
但是我的数据是这样的:
std::vector<std::string*>
Run Code Online (Sandbox Code Playgroud)
我一直在尝试将所述向量转换为 的第二种格式的正确格式execvp(),但不可避免地会出现以下错误:
command.cc:120:29: error: invalid conversion from ‘const char**’ to ‘char* const*’ [-fpermissive]
execvp(args[0], argv);
Run Code Online (Sandbox Code Playgroud)
我尝试了不同的变体,但它们都会导致此错误。这个错误让我很困惑,因为我不知道const*. 你怎么能有const*?我会考虑将 更改std::vector为其他类型,但这是一项任务,我实际上不允许更改它。下面是我用来尝试char*[]从向量创建一个的代码:
const size_t numArgs = _simpleCommands[i]->_arguments.size();
std::vector<const char*> args;
for(size_t j = 0; j < numArgs; ++j)
{
args.push_back(strPtrToCharPtr(_simpleCommands[i]->_arguments[i]));
}
const char** argv = new const char*[numArgs];
for(size_t j = 0; j < numArgs; ++j)
{
argv[j] = args[j];
}
execvp(args[0], argv);
Run Code Online (Sandbox Code Playgroud)
原型char* const argv[]意味着 thatargv是一个指向 的指针数组(的地址)char,数组中的指针不能修改,但它们指向的字符串可以修改。这与 a 不同char const **,a 是一个指向char 其字符不能被修改的指针。由于将其传递给可能修改数组中字符串的函数会违反const的限定符const char **,因此这是不允许的。(你可以用 来做到这一点const_cast,但这会解决错误的问题。)
由于execvp()它是一个非常古老的 UNIX 函数,并且今天不会有相同的接口,因此它没有任何参数来告诉操作系统有多少个参数,也不承诺不修改中的字符串内容数组。您可以通过将最后一个元素设置为 来终止数组NULL。
它\xe2\x80\x99与argv的参数格式类似main()。事实上,如果它是用 C 编写的,它就会成为您运行的程序的函数argv的参数。main()
这不是一个完整的解决方案,因为这是一项家庭作业,您想自己解决它,但您必须自己创建该数组。您可以通过以下方法来完成此操作:创建std::vector<char *> argv( args.size() + 1 ),将除最后一个元素之外的每个元素设置为.data()相应元素的指针args,并将最后一个元素设置为NULL。然后,传递argv.data()到execvp().
请注意,POSIX.1-2008 标准表示:
\n\n\n\n\n和
\nargv[]envp[]数组以及这些数组指向的字符串不得通过调用 exec 函数之一进行修改,除非替换过程映像。
因此,你应该能够摆脱掉const数组中字符串的 -ness,这一次。通常,您需要为数组中的每个常量字符串创建一个可修改的副本。
已经过去了足够长的时间,我\xe2\x80\x99m没有给出作业答案。一位评论者声称我的答案在 g++8 上不起作用,这意味着他们没有\xe2\x80\x99t 实现我正在考虑的相同算法。因此,发布完整的解决方案将会有所帮助。
\n\n这实际上解决了如何将 a 转换std::vector<std::string>为与 一起使用密切相关的问题execvp()。(A基本上永远不会正确,这里当然也不正确。如果您真的非常想要一个,请更改循环中std::vector<std::string*>的类型并取消引用。)sfor
#define _XOPEN_SOURCE 700\n// The next three lines are defensive coding:\n#define _POSIX_C_SOURCE 200809L\n#define _XOPEN_VERSION 700\n#define _XOPEN_UNIX 1\n\n#include <errno.h>\n#include <stdlib.h>\n#include <string>\n#include <unistd.h>\n#include <vector>\n\nint main()\n{\n const std::vector<std::string> cmdline{ "ls", "-al" };\n std::vector<const char*> argv;\n\n for ( const auto& s : cmdline ) {\n argv.push_back( s.data() );\n }\n argv.push_back(NULL);\n argv.shrink_to_fit();\n errno = 0;\n\n /* Casting away the const qualifier on the argument list to execvp() is safe\n * because POSIX specifies: "The argv[] [...] arrays of pointers and the\n * strings to which those arrays point shall not be modified by a call to\n * one of the exec functions[.]"\n */\n execvp( "/bin/ls", const_cast<char* const *>(argv.data()) );\n\n // If this line is reached, execvp() failed.\n perror("Error executing /bin/ls");\n return EXIT_FAILURE;\n}\nRun Code Online (Sandbox Code Playgroud)\n\n对此的另一个变化是编写一个转换函数,该函数返回std::vector<const char*>包含命令行参数的内容。由于有保证的复制省略,这同样有效。我通常喜欢使用 RIIA 和静态单一赋值进行编码,因此我发现返回一个生命周期自动管理的对象更优雅。在这种情况下, 的元素argv是对 中的字符串的弱引用cmdline,因此cmdline必须比 更长久argv。因为我们使用 C 风格的指针作为弱引用,所以 RIIA 在这里不太有效,我们仍然需要注意对象的生命周期。
#define _XOPEN_SOURCE 700\n#define _POSIX_C_SOURCE 200809L\n#define _XOPEN_VERSION 700\n#define _XOPEN_UNIX 1\n\n#include <errno.h>\n#include <stdlib.h>\n#include <string>\n#include <unistd.h>\n#include <vector>\n\nstd::vector<const char*> make_argv( std::vector<std::string>const& in )\n{\n std::vector<const char*> out;\n out.reserve( in.size() + 1 );\n\n for ( const auto& s : in ) {\n out.push_back( s.data() );\n }\n out.push_back(NULL);\n out.shrink_to_fit();\n\n return out; // Benefits from guaranteed copy elision.\n}\n\nint main()\n{\n const std::vector<std::string> cmdline{ "ls", "-al" };\n errno = 0;\n\n /* Casting away the const qualifier on the argument list to execvp() is safe\n * because POSIX specifies: "The argv[] [...] arrays of pointers and the\n * strings to which those arrays point shall not be modified by a call to\n * one of the exec functions[.]"\n */\n execvp( "/bin/ls", const_cast<char* const *>(make_argv(cmdline).data()) );\n\n // If this line is reached, execvp() failed.\n perror("Error executing /bin/ls");\n return EXIT_FAILURE;\n}\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
4748 次 |
| 最近记录: |