Is argv writeable?

A

Alexander Malkis

Supposed the ith actual argument argv has length n. Am I allowed to
write any string of length <=n to argv ?

Thanks in advance,
Alex.

PS. To email me, remove "loeschedies" from the email address given.
 
H

Howard

Alexander Malkis said:
Supposed the ith actual argument argv has length n. Am I allowed to
write any string of length <=n to argv ?

Thanks in advance,
Alex.

PS. To email me, remove "loeschedies" from the email address given.


If I recall, some systems do not even provide facilities for command-line
arguments. On systems that do, I think it's implementation and
system-specific as to how it's implemented.

But since there is no const in those arguments, I imagine it's theoretically
possible to write to those locations. The question is...why? They're not
outputs. Changing them won't change what was on the command line when the
program was invoked.

If you want to hold onto the array and modify its content, then make your
own copies of the strings in appropriate data structures (a vector of string
type seems appropriate), and manipulate those. That way you're sure it will
work (provided of course that you can get those command line arguments in
the first place).

-Howard
 
J

Jeff Schwab

Howard said:
Supposed the ith actual argument argv has length n. Am I allowed to
write any string of length <=n to argv ?

Thanks in advance,
Alex.

PS. To email me, remove "loeschedies" from the email address given.



If I recall, some systems do not even provide facilities for command-line
arguments. On systems that do, I think it's implementation and
system-specific as to how it's implemented.

But since there is no const in those arguments, I imagine it's theoretically
possible to write to those locations. The question is...why? They're not
outputs. Changing them won't change what was on the command line when the
program was invoked.


On some Unices, changing the contents of argv is a tricky way to change
the output shown by "ps", e.g. to display the progress of a process
instead of just its name. Of course, a more platform-specific hack
there never was...
 
G

Gianni Mariani

Howard said:
Supposed the ith actual argument argv has length n. Am I allowed to
write any string of length <=n to argv ?

Thanks in advance,
Alex.

PS. To email me, remove "loeschedies" from the email address given.



If I recall, some systems do not even provide facilities for command-line
arguments. On systems that do, I think it's implementation and
system-specific as to how it's implemented.

But since there is no const in those arguments, I imagine it's theoretically
possible to write to those locations. The question is...why? They're not
outputs. Changing them won't change what was on the command line when the
program was invoked.

If you want to hold onto the array and modify its content, then make your
own copies of the strings in appropriate data structures (a vector of string
type seems appropriate), and manipulate those. That way you're sure it will
work (provided of course that you can get those command line arguments in
the first place).


One example is this:

void main ( int argc, char **argv )
{

Widget shell, msg;
XtAppContext app;
XmString xmstr;
shell = XtAppInitialize (&app, "Memo", NULL, 0, &argc, argv, NULL,
NULL, 0 );

This is a standard way to initialize an Xt application where command
line arguments are consumed by the library and the function
XtAppInitialize is expected to remove the argv components it has used
leaving the application specific components for the application to consume.

....
 
H

Howard

One example is this:

void main ( int argc, char **argv )
{

Widget shell, msg;
XtAppContext app;
XmString xmstr;
shell = XtAppInitialize (&app, "Memo", NULL, 0, &argc, argv, NULL,
NULL, 0 );

This is a standard way to initialize an Xt application where command
line arguments are consumed by the library and the function
XtAppInitialize is expected to remove the argv components it has used
leaving the application specific components for the application to consume.

Interesting! Looks quite useful.

As I said, the signature sure seems to allow writing to argv. But does the
standard guarantee it?

-Howard
 
M

Minti

Gianni Mariani said:
Howard said:
Supposed the ith actual argument argv has length n. Am I allowed to
write any string of length <=n to argv ?

Thanks in advance,
Alex.

PS. To email me, remove "loeschedies" from the email address given.



If I recall, some systems do not even provide facilities for command-line
arguments. On systems that do, I think it's implementation and
system-specific as to how it's implemented.

But since there is no const in those arguments, I imagine it's theoretically
possible to write to those locations. The question is...why? They're not
outputs. Changing them won't change what was on the command line when the
program was invoked.

If you want to hold onto the array and modify its content, then make your
own copies of the strings in appropriate data structures (a vector of string
type seems appropriate), and manipulate those. That way you're sure it will
work (provided of course that you can get those command line arguments in
the first place).


One example is this:

void main ( int argc, char **argv )

^^^^

void(?)
 
M

Mike Wahler

Howard said:
Interesting! Looks quite useful.

As I said, the signature sure seems to allow writing to argv. But does the
standard guarantee it?

The C (99) standard does explicitly make this gurantee, but
I don't see it in the C++(98) standard. (Or maybe I'm looking
in the wrong place ... wouldn't be the first time. :) )

-Mike
 
D

Default User

Mike said:
The C (99) standard does explicitly make this gurantee, but
I don't see it in the C++(98) standard. (Or maybe I'm looking
in the wrong place ... wouldn't be the first time. :) )

As did the C89 standard before it. I wonder why they left out that bit
of boilerplate, if it's missing from the final standard (I just have the
last draft).

The C standard says:

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.



Brian Rodenborn
 
M

Mike Wahler

Default User said:
As did the C89 standard before it. I wonder why they left out that bit
of boilerplate, if it's missing from the final standard (I just have the
last draft).

The C standard says:

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.

Yes, I found that, but no similar wording in the C++ standard.

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top