为什么ARGV参数execvp不是const的? [英] Why is argv parameter to execvp not const?

查看:103
本文介绍了为什么ARGV参数execvp不是const的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

execvp是这样定义:

execvp is defined thus:

int execvp(const char *file, char *const argv[]);

从正在使用precludes code像这样的:

Which precludes code such as this from being used:

const char* argv[] = {"/bin/my", "command", "here", NULL};
execvp(argv[0], argv);

这是一个意外遗漏?它是安全的const_cast会解决此问题?或者做一些execvp实现实际上是在内存潦草?

Was this an accidental omission? Is it safe to const_cast around this? Or do some execvp implementations actually scribble on that memory?

推荐答案

的POSIX规范说(的 http://pubs.opengroup.org/onlinepubs/009604499/functions/exec.html ):

The POSIX spec says (http://pubs.opengroup.org/onlinepubs/009604499/functions/exec.html):

的argv [] envp [] 指针数组和字符串到这些阵列点不应通过调用进行修改的Exec功能之一,除了更换处理图像的结果。

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.

<击>我觉得缺失(或错位)常量只是一个历史怪胎。

I think the missing (or misplaced) const is simply an historical oddity.

这篇关于为什么ARGV参数execvp不是const的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆