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

查看:29
本文介绍了为什么`execvp` 采用`char *const argv[]`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道 const-ness 中两个 exec 函数之间是否存在不同的原因,或者这是否只是单一 Unix 规范中的一个错误:

摘自 Linux 联机帮助页,它似乎与 单一 Unix 规范一致,这里是 exec 的两个版本:

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

execlp 把它的参数当作 const char * ,它需要两个或多个.C 中的 const 承诺函数不会更改指向的数据,在这种情况下,是构成字符串的实际字符 (char).

execvp 取而代之的是将其参数作为指针数组.然而,与您所期望的那样指向 const char * 的指针数组不同,const 关键字位于不同的位置——这对 C 非常重要.execvp 说它可以很好地修改字符串中的字符,但它承诺不会修改数组——即指向字符串的指针.所以,换句话说,

int fake_execvp(const char *file, char *const argv[]) {argv[0] = "其他字符串";/* 这是一个错误 */argv[0][0] = 'f';/* 将第一个字母更改为 'f':这完全没问题!*//* ⋮ */}

特别是,这使得使用 C++ 的 std::stringto_cstr() 方法调用 execvp 变得困难(从技术上说是禁止的),该方法返回 const char *.

看起来execvp真的应该采用const char *const argv[],换句话说,它应该保证不做上述任何一个更改.

解决方案

引用您链接的页面:

<块引用>

关于 argv[]envp[] 是常量的声明包含在向未来的语言绑定作者明确说明这些对象是完全不变的.由于 ISO C 的限制标准,不可能在标准 C 中陈述这个想法.指定两个级别的 const- 限定 argv[]envp[] exec 函数的参数可能看起来很自然选择,鉴于这些函数不修改数组指针或函数指向的字符,但是这个将不允许现有的正确代码.

基本上 execlpexecvp 上的 const 限定是完全兼容的,因为它们对相应的参数指定了相同的限制.>

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天全站免登陆