This thread on the comp.lang.c.moderated newsgroup discusses the issue at length from a C standard point of view, including a citation showing that the contents of the argv arrays (rather than the argv pointer itself, if e.g. you took an address &argv
and stored that) last until "program termination", and an assertion that it is "obvious" that program termination has not yet occurred in a way relevant to this while the atexit-registered functions are executing:
The program has not terminated during atexit-registered
function processing. We thought that was pretty obvious.
(I'm not sure who Douglas A. Gwyn is, but it sounds like "we" means the C standard committee?)
The context of the discussion was mainly concerning storing a copy of the pointer argv[0]
(program name).
The relevant C standard text is 5.1.2.2.1:
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.
Of course, C++ is not C, and its standard may subtly differ on this issue or not address it.
if (largs[0] && largs[1])
. – Thievelargs[1]
would be null ifargc==1
? The OP just needs to make a globalint largsc;
and assign itlargsc = argc
in main() just like he did forlargs
. Thenfunction_1()
has access to not only the program'sargv
, but also itsargc
. – Californiaargv[argc]
shall be a null pointer." Of course, you can also do as you suggest, but it's often convenient to take advantage of the fact thatargv
is guaranteed to be null-terminated. – Thievechar **p; for (p = argv; *p; p++) process_arg(*p);
. On a side note, it appears that ISO C permitsargc
to be 0, so unless you are in an environment that guarantees that won't happen, you can't even be sure thatargv[0]
contains a string. – Thievegetopt
and it's many descendants and distant relations takeargc
andargv
as arguments so there is a long standing tradition of accessing the strings pointed to byargv
in functions other thanmain
. – Snowbirdchar **p; for (p = argv+1; *p; p++) process_arg(*p);
– Crossgarnet