argv数组是否可以修改? [英] Is argv array modifiable ?

查看:62
本文介绍了argv数组是否可以修改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设主函数定义为

int main(int argc,char * argv []){/*...*/}


那么,是否允许修改argv数组?标准说

"参数argc和argv以及

argv数组指向的字符串应由程序修改,[...]。根据

我的标准读取,例如,++ argv和++ argv [0] [0]

都是允许的,但不是++ argv [因为它没有说明argv数组本身的

。我的解释是否正确?

解决方案

mnaydin说:

假设主要功能是用
int main定义(int argc,char * argv []){/*...* /}
那么,是否允许修改argv数组?标准说
参数argc和argv以及
argv数组指向的字符串应该可由程序修改,[...]。根据我对标准的解读,例如,++ argv和++ argv [0] [0]
都是允许的,但不是++ argv [0],因为它没有说明
argv数组本身。我的解释是否正确?




< caveat class ="这是来自内存,而非标准">

我相信如此是的你可以修改argv,因为你得到了一个

的来电者价值的副本,那么为什么来电者要关心呢?b $ b你用它做什么?你可以修改每个

字符串的内容,因为没有特别的理由禁止你

,只要你不试图拉伸字符串 - 即

乱写或超过空终止符。但是对于你们知道的所有人来说,实现可能已经使用动态分配

来获取存储这些字符串所需的内存,并且

可能

分配器没有备用指针值的备份 - 所以(如果我没记错的话)标准

如果你<不提供任何行为保证

搞乱那些指针。

< / caveat>


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)


2005-12-15,Richard Heathfield <在***** @ invalid.invalid>写道:

mnaydin说:

假设main函数定义为
int main(int argc,char * argv []){/ * ... * /}
那么,是否允许修改argv数组?标准说
参数argc和argv以及
argv数组指向的字符串应该可由程序修改,[...]。根据我对标准的解读,例如,++ argv和++ argv [0] [0]
都是允许的,但不是++ argv [0],因为它没有说明
argv数组本身。我的解释是否正确?



< caveat class ="这是来自记忆,而不是标准>
我相信是的,是的。您可以修改argv,因为您获得了调用者值的副本,那么调用者为什么要关心您使用它做什么?你可以修改每个
字符串的内容,因为没有特别的理由禁止你
,只要你不试图拉伸字符串 - 即
涂鸦或者通过空终止符。但是对于所有人都知道,实现可能已经使用动态分配来获取存储这些字符串所需的内存,并且
可能没有由如果你把这些指针弄得乱七八糟,就不会提供任何行为保证。
< / caveat>




你能交换其中两个吗? [假设你想把所有的论点

从'' - ''开头到数组的开头]


Jordan Abel写道:

2005-12-15,Richard Heathfield< in ***** @ invalid.invalid>写道:

mnaydin说:

假设main函数定义为
int main(int argc, char * argv []){/*...*/}
那么,是否允许修改argv数组?标准说
参数argc和argv以及
argv数组指向的字符串应该可由程序修改,[...]。根据我对标准的解读,例如,++ argv和++ argv [0] [0]
都是允许的,但不是++ argv [0],因为它没有说明
argv数组本身。我的解释是否正确?



< caveat class ="这是来自记忆,而不是标准>
我相信是的,是的。您可以修改argv,因为您获得了调用者值的副本,那么调用者为什么要关心您使用它做什么?你可以修改每个
字符串的内容,因为没有特别的理由禁止你
,只要你不试图拉伸字符串 - 即
涂鸦或者通过空终止符。但是对于所有人都知道,实现可能已经使用动态分配来获取存储这些字符串所需的内存,并且
可能没有由如果你把这些指针弄得乱七八糟,就不会提供任何行为保证。
< / caveat>



你可以交换其中两个吗? [假设您想将所有参数
以'' - ''开头到数组的开头]




不可靠。当有人说'argv'时,有三种不同的东西可以讨论:


- 函数参数变量:这是可修改的。 />

- 单个指针argv [0],argv [1],...

标准没有说明这些是否可以修改。


- 首字符为* argv [0]的字符串,

* argv [1],...标准说这些是可修改的。


第5.1.2.2.1节,第2段,最终约束。


-

Eric Sosman
es ***** @ acm-dot-org.inva lid

Assume the main function is defined with
int main(int argc, char *argv[]) { /*...*/ }

So, is it permitted to modify the argv array? The standard says
"The parameters argc and argv and the strings pointed to by the
argv array shall be modifiable by the program,[...]". According to
my reading of the standard, for example, ++argv and ++argv[0][0]
are both permitted, but not ++argv[0] because it says nothing about
the argv array itself. Is my interpretation correct ?

解决方案

mnaydin said:

Assume the main function is defined with
int main(int argc, char *argv[]) { /*...*/ }

So, is it permitted to modify the argv array? The standard says
"The parameters argc and argv and the strings pointed to by the
argv array shall be modifiable by the program,[...]". According to
my reading of the standard, for example, ++argv and ++argv[0][0]
are both permitted, but not ++argv[0] because it says nothing about
the argv array itself. Is my interpretation correct ?



<caveat class="this is from memory, not the Standard">
I believe so, yes. You can modify argv because you get a
copy of the caller''s value, so why should the caller care
what you do with it? You can modify the contents of each
string because there''s no particular reason to forbid you
to, so long as you don''t try to stretch the string - i.e.
scribble over or past the null terminator. But for all you
know, the implementation might have used dynamic allocation
to get the memory it needs for storing those strings, and
might have no spare copy of the pointer values returned by
the allocator - so (if I recall correctly) the Standard
doesn''t offer any behaviour guarantees whatsoever if you
mess with those pointers.
</caveat>

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


On 2005-12-15, Richard Heathfield <in*****@invalid.invalid> wrote:

mnaydin said:

Assume the main function is defined with
int main(int argc, char *argv[]) { /*...*/ }

So, is it permitted to modify the argv array? The standard says
"The parameters argc and argv and the strings pointed to by the
argv array shall be modifiable by the program,[...]". According to
my reading of the standard, for example, ++argv and ++argv[0][0]
are both permitted, but not ++argv[0] because it says nothing about
the argv array itself. Is my interpretation correct ?



<caveat class="this is from memory, not the Standard">
I believe so, yes. You can modify argv because you get a
copy of the caller''s value, so why should the caller care
what you do with it? You can modify the contents of each
string because there''s no particular reason to forbid you
to, so long as you don''t try to stretch the string - i.e.
scribble over or past the null terminator. But for all you
know, the implementation might have used dynamic allocation
to get the memory it needs for storing those strings, and
might have no spare copy of the pointer values returned by
the allocator - so (if I recall correctly) the Standard
doesn''t offer any behaviour guarantees whatsoever if you
mess with those pointers.
</caveat>



Can you swap two of them? [suppose you want to bring all arguments
starting with ''-'' to the beginning of the array]


Jordan Abel wrote:

On 2005-12-15, Richard Heathfield <in*****@invalid.invalid> wrote:

mnaydin said:

Assume the main function is defined with
int main(int argc, char *argv[]) { /*...*/ }

So, is it permitted to modify the argv array? The standard says
"The parameters argc and argv and the strings pointed to by the
argv array shall be modifiable by the program,[...]". According to
my reading of the standard, for example, ++argv and ++argv[0][0]
are both permitted, but not ++argv[0] because it says nothing about
the argv array itself. Is my interpretation correct ?



<caveat class="this is from memory, not the Standard">
I believe so, yes. You can modify argv because you get a
copy of the caller''s value, so why should the caller care
what you do with it? You can modify the contents of each
string because there''s no particular reason to forbid you
to, so long as you don''t try to stretch the string - i.e.
scribble over or past the null terminator. But for all you
know, the implementation might have used dynamic allocation
to get the memory it needs for storing those strings, and
might have no spare copy of the pointer values returned by
the allocator - so (if I recall correctly) the Standard
doesn''t offer any behaviour guarantees whatsoever if you
mess with those pointers.
</caveat>


Can you swap two of them? [suppose you want to bring all arguments
starting with ''-'' to the beginning of the array]



Not reliably. There are three different things one might
be talking about when one says `argv'':

- The function parameter variable: This is modifiable.

- The individual pointers argv[0], argv[1], ... The
Standard says nothing about whether these are modifiable.

- The strings whose first characters are *argv[0],
*argv[1], ... The Standard says these are modifiable.

Section 5.1.2.2.1, paragraph 2, final constraint.

--
Eric Sosman
es*****@acm-dot-org.invalid


这篇关于argv数组是否可以修改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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