Output

Discussion in 'C Programming' started by Kuku, Aug 9, 2005.

  1. Kuku

    Kuku Guest

    Would the following program give the same output at all times


    int main(int argc, char *argv[])
    {
    strcpy(argv[0], "Hello");
    strcpy(argv[1],"Good Morning");
    printf("%s\n%s\n",argv[0],argv[1]);
    return 0;
    }
     
    Kuku, Aug 9, 2005
    #1
    1. Advertising

  2. In article <>,
    Kuku <> wrote:

    > strcpy(argv[0], "Hello");
    > strcpy(argv[1],"Good Morning");


    Consider whether argv[0] and argv[1] are guaranteed to point to enough
    space, or even any space at all.

    -- Richard
     
    Richard Tobin, Aug 9, 2005
    #2
    1. Advertising

  3. In article <>,
    Kuku <> wrote:
    >Would the following program give the same output at all times
    >
    >
    >int main(int argc, char *argv[])
    >{
    > strcpy(argv[0], "Hello");
    > strcpy(argv[1],"Good Morning");
    > printf("%s\n%s\n",argv[0],argv[1]);
    > return 0;
    >}
    >


    Yes. I don't see any time dependency in the above code.

    I would assume that, whatever it does (and that question is left open), it
    would do the same thing in the morning, in the evening, and at all other
    times (on the same machine/compiler/etc combination, of course).
     
    Kenny McCormack, Aug 9, 2005
    #3
  4. Kuku

    Richard Bos Guest

    (Kenny McCormack) wrote:

    > In article <>,
    > Kuku <> wrote:
    > >Would the following program give the same output at all times
    > >
    > >int main(int argc, char *argv[])
    > >{
    > > strcpy(argv[0], "Hello");
    > > strcpy(argv[1],"Good Morning");
    > > printf("%s\n%s\n",argv[0],argv[1]);
    > > return 0;
    > >}

    >
    > Yes. I don't see any time dependency in the above code.


    Since the code could easily invoke undefined behaviour (e.g., when
    called without any command line arguments), this does not mean that it
    will behave the same at any given time.

    For example, it's quite possible that a system that is starved for
    resources in the afternoon (when everybody has their Outhouse Express
    open and polls the server every 60 seconds[1]) is a bit more roomy in
    the morning, when only half the workforce is in yet. The result could be
    that writing through a wild pointer goes unnoticed at 8:37, but
    accidentally writes to the flag that causes replies to newsgroup trolls
    at 14:52.

    (And yes, that program could write through a wild pointer. argc is
    allowed to be 0.)

    Richard

    [1] Don't ask. Just don't.
     
    Richard Bos, Aug 9, 2005
    #4
  5. Kuku

    Lew Pitcher Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Kuku wrote:
    > Would the following program give the same output at all times
    >
    >
    > int main(int argc, char *argv[])
    > {
    > strcpy(argv[0], "Hello");
    > strcpy(argv[1],"Good Morning");
    > printf("%s\n%s\n",argv[0],argv[1]);
    > return 0;
    > }


    Beyond Richard Bos' reply wrt the number of arguments, there are other
    'undefined behaviours' in this code that could cause it to vary it's output.

    Each argv[] is a pointer to either NULL (in the case of argv[argc]) or a
    string. Let us ignore the special case of argv[argc] for the moment.

    The string that the argv[n] pointer points to is only guaranteed to be
    as long as necessary to store the passed argument. This /can/ be smaller
    than the strings you write into the storage (you are using strcpy(), you
    are writing strings, not copying pointers).

    So, if argv[0] pointed to a two character array (like "a"), then your
    strcpy(argv[0], "Hello");
    replaces those two characters, then promptly goes out-of-bounds and
    invokes undefined behaviour. Similar behaviour comes from your
    strcpy(argv[1], "Good Morning");
    statement.

    Since argc and argv[] reflect external values, supplied (somehow) prior
    to the initiation of your program, you have no programmatic control over
    their content (at least, not in /this/ program), and can (and will) vary
    depending on external circumstances.

    In other words, at 09:00, your program may be invoked with
    argc == 2
    argv[0] -> "a"
    argv[1] -> "b"
    argv[2] == NULL

    while at 09:03, your program is invoked with
    argc == 1
    argv[0] -> "some program Name"
    argv[1] == NULL

    and at 09:05, your program is invoked with
    argc == 3
    argv[0] -> "a big program name"
    argv[1] -> "something much larger than I need"
    argv[2] -> ""
    argv[3] == NULL

    And, /that/ is time-variant undefined behaviour.




    - --

    Lew Pitcher, IT Specialist, Enterprise Data Systems
    Enterprise Technology Solutions, TD Bank Financial Group

    (Opinions expressed here are my own, not my employer's)
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.4 (MingW32)

    iD8DBQFC+LLxagVFX4UWr64RAr5LAKC6bz9iGVC5IBcn1nQ4kSCxsTHqLgCeMk35
    NDJu9FtCPmm6cEov1zkK+e4=
    =S06b
    -----END PGP SIGNATURE-----
     
    Lew Pitcher, Aug 9, 2005
    #5
  6. Kuku

    SM Ryan Guest

    "Kuku" <> wrote:
    # Would the following program give the same output at all times

    Why do you even care?

    # int main(int argc, char *argv[])
    # {
    # strcpy(argv[0], "Hello");
    # strcpy(argv[1],"Good Morning");
    # printf("%s\n%s\n",argv[0],argv[1]);
    # return 0;
    # }

    #include <stdlib.h>
    #include <stdio.h>

    int main(int argc0,char *argv[]) {
    int argc = argc0<2 ? 2 : argc0,i;
    char **argv = malloc((argc+1)*sizeof(char*));
    for (i=0; i<argc0; i++) argv = argv0;
    for (i=argc0+1; i<argc; i++) argv = 0;
    argv[argc] = 0;
    argv[0] = "Hello";
    argv[1] = "Good Morning";
    printf("%s\n%s\n",argv[0],argv[1]);
    return 0;
    }

    It's not particularly difficult to write programs that avoid
    implementation dependent behaviour unless they have to actually
    do so. So why not program for clarity?

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    I ASSURE YOU WE'RE OPEN!
     
    SM Ryan, Aug 9, 2005
    #6
    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. John Bentley

    Output / Debug window output bug?

    John Bentley, Sep 10, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    596
    John Bentley
    Sep 10, 2003
  2. chuck amadi
    Replies:
    1
    Views:
    491
    Larry Bates
    Jun 23, 2004
  3. Paul
    Replies:
    1
    Views:
    430
  4. jmr
    Replies:
    2
    Views:
    337
  5. pratap
    Replies:
    3
    Views:
    460
    James Kanze
    Jul 19, 2007
Loading...

Share This Page