是的argv [N]写吗? [英] Is argv[n] writable?
问题描述
C11 5.1.2.2.1 / 2说:
C11 5.1.2.2.1/2 says:
参数 ARGC
和的argv
和琴弦由的argv 指了指code>阵列应
是可修改的程序,并保留程序之间的最后存储的值
启动和终止程序。
The parameters
argc
andargv
and the strings pointed to by theargv
array shall be modifiable by the program, and retain their last-stored values between program startup and program termination.
这个我间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
andargv[x][y]
but notargv[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
"The parameters argc and argv and the strings pointed to by the argv array shall be modifiable by the program, and retain their last-stored values between program startup and program termination." C11dr §5.1.2.2.1 2
回想一下,常量
多年后,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 []
或<其他一些签名code>常量,缺乏在
未完成指定字符常量
的* 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
Undefined behavior is otherwise indicated in this International Standard by the words ‘‘undefined behavior’’ or by the omission of any explicit definition of behavior" §4 2
:
的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]写吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!