execvp 是这样定义的:
int execvp(const char *file, char *const argv[]);
这会阻止使用这样的代码:
const char* argv[] = {"/bin/my", "command", "here", NULL};
execvp(argv[0], argv);
这是意外遗漏吗?围绕这个进行 const_cast 安全吗?还是某些 execvp 实现实际上在该内存上涂鸦?
最佳答案
POSIX 规范说 ( http://pubs.opengroup.org/onlinepubs/009604499/functions/exec.html ):
The
argv[]
andenvp[]
arrays of pointers and the strings to which those arrays point shall not be modified by a call to one of the exec functions, except as a consequence of replacing the process image.
我认为丢失(或错放)const
只是一个历史怪事。
关于c - 为什么 execvp 的 argv 参数不是 const?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10456043/