本文介绍了是的argv [N]写吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C11 5.1.2.2.1 / 2说:

C11 5.1.2.2.1/2 says:

参数 ARGC 的argv 和琴弦由的argv 阵列应
  是可修改的程序,并保留程序之间的最后存储的值
  启动和终止程序。

这个我间pretation是:它规定:

My interpretation of this is that it specifies:

int main(int argc, char **argv)
{
    if ( argv[0][0] )
        argv[0][0] = 'x';   // OK

    char *q;
    argv = &q;              // OK
}

但它并没有说任何事情:

however it does not say anything about:

int main(int argc, char **argv)
{
    char buf[20];
    argv[0] = buf;
}

的argv [0] = BUF; 允许

我可以看到(至少)两个可能的参数:

I can see (at least) two possible arguments:


  • 特意提到以上报价的argv 的argv [X] [Y] 而不是的argv [X] ,这样的意图是,它是不可修改

  • 的argv 是一个指向非 - 常量对象,所以在没有具体的措辞相反我们应该假设他们是可以修改的对象。

  • The above quote deliberately mentioned argv and argv[x][y] but not argv[x], so the intent was that it is not modifiable
  • argv is a pointer to non-const objects, so by in the absence of specific wording to the contrary, we should assume they are modifiable objects.

推荐答案

IMO,code像的argv [1] =123; 是UB

IMO, code like argv[1] = "123"; is UB.

参数argc和argv与琴弦由argv数组指出,应
  是可修改的程序,并保留程序之间的最后存储的值
  启动和终止程序。C11dr§5.1.2.2.12

回想一下,常量多年后,C'S创作走进℃。

Recall that const came into C many years after C's creation.

就像的char * s =ABC; 是有效的,当它应该是为const char * s =ABC; 。不需要进行必要常量其他太多现有的code将引进常量的被打破

Much like char *s = "abc"; is valid when it should be const char *s = "abc";. The need for const was not required else too much existing code would have be broken with the introduction of const.

同样,即使的argv 今天应该算是 char * const的的argv [] 或常量,缺乏字符常量的* argv的[] 未完成指定常量的-ness需求的argv 的argv [] 的argv [] [] 。在常量 -ness需求将需要通过规范来驱动。

Likewise, even if argv today should be considered char * const argv[] or some other signature with const, the lack of const in the char *argv[] does not complete specify the const-ness needs of the argv, argv[], or argv[][]. The const-ness needs would need to be driven by the spec.

从我读,因为规范是在这个问题上沉默,这是UB。

From my reading, since the spec is silent on the issue, it is UB.

未定义行为,否则该国际标准的文字''未定义行为'或行为的任何明确定义§42

的main()是一个非常特殊的功能是什么C.允许的是在其他功能上可能会或可能不会在被允许的main()。的C规格细节的属性有关它的参数,鉴于签署 INT ARGC,CHAR *的argv [] 不应该需要。 的main(),不像用C等功能,可以有一个备用签名 INT主要(无效)和潜在的其他人。 的main()不是折返。像C规格超出它的方式来详细说一下可以修改: ARGC 的argv 的argv [] [] ,它是合理的问题,如果的argv [] 是可以修改的,因为它从规范声称$ C遗漏$ C能。

main() is a very special function is C. What is allowable in other functions may or may not be allowed in main(). The C spec details attributes about its parameters that given the signature int argc, char *argv[] that shouldn't need. main(), unlike other functions in C, can have an alternate signature int main(void) and potentially others. main() is not reentrant. As the C spec goes out of its way to detail what can be modified: argc, argv, argv[][], it is reasonable to question if argv[] is modifiable due to its omission from the spec asserting that code can.

鉴于专业的main()并指定遗漏了的argv [] 的修改,保守程序员把这个作为greyness UB,有待未来的C规格说明。

Given the specialty of main() and the omission of specifying that argv[] as modifiable, a conservative programmer would treat this greyness as UB, pending future C spec clarification.

如果的argv [I] 是修改一个给定的平台上,肯定的范围I 不应该超过 ARGC-1

If argv[i] is modifiable on a given platform, certainly the range of i should not exceed argc-1.

由于 ARGV [ARGC] 应是一个空指针,assignining ARGV [ARGC] 其他东西比 NULL 似乎是违反

As "argv[argc] shall be a null pointer", assignining argv[argc] to something other than NULL appears to be a violation.

虽然字符串修改,code不应超过原始字符串的长度。

Although the strings are modifiable, code should not exceed the original string's length.

char *newstr = "abc";
if (strlen(newstr) <= strlen(argv[1]))
  strcpy(argv[1], newstr);

这篇关于是的argv [N]写吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 21:32