为什么`execvp`采取`char * const的的argv []`? [英] Why does `execvp` take a `char *const argv[]`?

查看:184
本文介绍了为什么`execvp`采取`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是,该功能不会改变指向的数据,在这种情况下的实际字符(字符)构成的字符串。

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 as const 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 to const char * as you'd expect, the const 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's to_cstr() method, which returns const char *.

It seems like execvp really ought to take const 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[] and envp[] 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 of const- qualification for the argv[] and envp[] 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 on execlp and execvp are completely compatible in the sense that they specify identical limitations on the corresponding arguments.

这篇关于为什么`execvp`采取`char * const的的argv []`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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