Output

K

Kuku

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;
}
 
R

Richard Tobin

Kuku said:
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
 
K

Kenny McCormack

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).
 
R

Richard Bos

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.
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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-----
 
S

SM Ryan

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

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top