Is argv writeable?

Discussion in 'C++' started by Alexander Malkis, May 20, 2004.

  1. 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.
    Alexander Malkis, May 20, 2004
    #1
    1. Advertising

  2. Alexander Malkis

    Howard Guest

    "Alexander Malkis" <-sb.de> wrote in message
    news:c8iguc$2eieo$-saarland.de...
    > 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
    Howard, May 20, 2004
    #2
    1. Advertising

  3. Alexander Malkis

    Jeff Schwab Guest

    Howard wrote:
    > "Alexander Malkis" <-sb.de> wrote in message
    > news:c8iguc$2eieo$-saarland.de...
    >
    >>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...
    Jeff Schwab, May 20, 2004
    #3
  4. Howard wrote:
    > "Alexander Malkis" <-sb.de> wrote in message
    > news:c8iguc$2eieo$-saarland.de...
    >
    >>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.

    ....
    Gianni Mariani, May 20, 2004
    #4
  5. Alexander Malkis

    Default User Guest

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



    Yes.




    Brian Rodenborn
    Default User, May 20, 2004
    #5
  6. Alexander Malkis

    Howard Guest

    "Gianni Mariani" <> wrote in message
    news:c8ilrq$...

    > > The question is...why?


    >
    > 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
    Howard, May 20, 2004
    #6
  7. Alexander Malkis

    Minti Guest

    Gianni Mariani <> wrote in message news:<c8ilrq$>...
    > Howard wrote:
    > > "Alexander Malkis" <-sb.de> wrote in message
    > > news:c8iguc$2eieo$-saarland.de...
    > >
    > >>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(?)

    --
    Imanpreet Singh Arora
    Minti, May 20, 2004
    #7
  8. Alexander Malkis

    Mike Wahler Guest

    "Howard" <> wrote in message
    news:lV5rc.8685$...
    >
    > "Gianni Mariani" <> wrote in message
    > news:c8ilrq$...
    >
    > > > The question is...why?

    >
    > >
    > > 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?


    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
    Mike Wahler, May 21, 2004
    #8
  9. Alexander Malkis

    Default User Guest

    Mike Wahler wrote:

    > 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
    Default User, May 21, 2004
    #9
  10. Alexander Malkis

    Mike Wahler Guest

    "Default User" <> wrote in message
    news:...
    > Mike Wahler wrote:
    >
    > > 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.


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

    -Mike
    Mike Wahler, May 23, 2004
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hazz
    Replies:
    4
    Views:
    3,871
  2. Bret

    char **argv vs. char* argv[]

    Bret, Aug 31, 2003, in forum: C Programming
    Replies:
    21
    Views:
    4,583
    Richard Heathfield
    Sep 3, 2003
  3. Steve Chaplin

    How to create a writeable buffer?

    Steve Chaplin, Nov 20, 2006, in forum: Python
    Replies:
    0
    Views:
    838
    Steve Chaplin
    Nov 20, 2006
  4. Tzury Bar Yochay
    Replies:
    4
    Views:
    570
    John Machin
    Sep 21, 2008
  5. Replies:
    2
    Views:
    166
Loading...

Share This Page