为什么`execvp`采取`char * const的的argv []`? [英] Why does `execvp` take a `char *const argv[]`?
问题描述
我想知道,如果在常量
-ness不同的,如果这仅仅是在单一Unix规格中的错误,二exec函数之间的一个原因:
从Linux手册页,这似乎与单一Unix规格对齐,这里摘录是 EXEC的两个版本
:
INT execlp(为const char * 档案,为const char * ARG ,...);结果
块引用>
INT execvp(为const char * 档案,char * const的的argv的 []);
execlp
接受它的参数为为const char *
,它需要两个或更多的人。常量
在C是,该功能不会改变指向的数据,在这种情况下的实际字符(字符$ C $承诺C>)构成的字符串。
execvp
而不是接受它的参数作为指针数组。然而,而不是一个指针数组为const char *
正如你所期望的,常量
关键字是在不同点 - 这重要颇有几分C.execvp
是说很可能修改字符字符串,但承诺不修改数组,也就是说,该指针串。因此,换句话说,INT fake_execvp(为const char *文件,char * const的的argv []){
的argv [0] =其他一些字符串; / *这是一个错误* /
的argv [0] [0] = f的; / *改第一个字母'F':这是绝对OK! * /
/ * *⋮/
}在特定的,这使得它很难(技术上,禁止)来调用execvp使用C ++小号的
的std :: string的
to_cstr()
方法,它返回为const char *
。看起来
execvp
真的应该采取为const char * const的的argv []
,换句话说,它应该承诺不办的上述变化无论是。解决方案要引用您链接的页面:
关于
的argv []
和envp []
是常量包含的陈述书
作出明确语言绑定未来的作家,这些
对象是完全不变。由于ISO C的限制
标准,这是不可能的状态在标准C.这一想法
指定常量
的两个层次 - 对资质的argv []
和
envp []
的执行函数的参数似乎是自然的
选择,因为这些功能不修改的任何数组
指针或字符到的功能分,但此
将不允许现有的正确code。
块引用>在基本上
常量
资质execlp
和execvp
在这个意义上,它们在相应的参数指定相同的限制,完全兼容。I'm wondering if there is a reason between two exec functions differing in
const
-ness, of if this is just a bug in the Single Unix Spec:Excerpting from the Linux manpage, which appears to align with the Single Unix Specification, here are a two versions of
exec
:int execlp(const char *file, const char *arg, ...);
int execvp(const char *file, char *const argv[]);
execlp
takes its arguments asconst char *
, and it takes two or more of them.const
in C is a promise that the function will not change the pointed-to data, in this case the actual characters (char
) that make up the string.
execvp
instead takes its arguments as an array of pointers. However, instead of an array of pointers toconst char *
as you'd expect, theconst
keyword is in a different spot—and this matters quite a bit to C.execvp
is saying it may well modify the characters in the strings, but it promises not to modify the array—that is, the pointers to the strings. So, in other words,int fake_execvp(const char *file, char *const argv[]) { argv[0] = "some other string"; /* this is an error */ argv[0][0] = 'f'; /* change first letter to 'f': this is perfectly OK! */ /* ⋮ */ }
In particular, this makes it hard (technically, prohibited) to call execvp using C++'s
std::string
'sto_cstr()
method, which returnsconst char *
.It seems like
execvp
really ought to takeconst char *const argv[]
, in other words, it ought to promise not to do either of the above changes.解决方案To quote the page you link:
The statement about
argv[]
andenvp[]
being constants is included to make explicit to future writers of language bindings that these objects are completely constant. Due to a limitation of the ISO C standard, it is not possible to state that idea in standard C. Specifying two levels ofconst
- qualification for theargv[]
andenvp[]
parameters for the exec functions may seem to be the natural choice, given that these functions do not modify either the array of pointers or the characters to which the function points, but this would disallow existing correct code.Basically the
const
qualification onexeclp
andexecvp
are completely compatible in the sense that they specify identical limitations on the corresponding arguments.这篇关于为什么`execvp`采取`char * const的的argv []`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!