c - 为什么 execvp 的 argv 参数不是 const?

标签 c libc execvp

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[] and envp[] 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/

相关文章:

c - 从列专业转移到行专业

c - 我的 C 代码有什么问题? (大数的主要因素)

c 读取非 ASCII 字符

gcc -/lib/i386-linux-gnu/libc.so.6、/lib/x86_64-linux-gnu/libc.so.6 和/usr/lib/x86_64-linux-gnu/libc.so 有什么区别?

c - 为什么这段计算二次方程解的代码不起作用?

c - 替换极其缓慢的 pow() 函数

rust - Rust libc crate 是否禁止编译自定义 panic 处理程序?

c++ - 用 forkpty 和 execvp fork bash 后,bash 不响应 SIGINT

c - valgrind 对 execvp 大小 1 的无效读取

c - 将字符添加到现有的字符/字符指针数组?