Reputation: 605
Is it safe to use the argv
pointer globally? Or is there a circumstance where it may become invalid?
i.e: Is this code safe?
char **largs;
void function_1()
{
printf("Argument 1: %s\r\n",largs[1]);
}
int main(int argc,char **argv)
{
largs = argv;
function_1();
return 1;
}
Upvotes: 30
Views: 4470
Reputation: 15080
It should be safe so long as main()
function does not exit. A few examples of things that can happen after main()
exits are:
main()
Stored argv
must not be used in those.
The reference doesn't say anything which would give a reason to assume that the lifetimes of the arguments to main()
function differ from the general rules for lifetimes of function arguments.
So long as argv
pointer itself is valid, the C/C++ runtime must guarantee that the content to which this pointer points is valid (of course, unless something corrupts memory). So it must be safe to use the pointer and the content that long. After main()
returns, there is no reason for the C/C++ runtime to keep the content valid either. So the above reasoning applies to both the pointer and the content it points to.
Upvotes: 19
Reputation: 39050
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.
Upvotes: 5
Reputation: 65720
Yes, it is safe to use argv
globally; you can use it as you would use any char**
in your program. The C99 standard even specifies this:
The parameters
argc
andargv
and the strings pointed to by theargv
array shall be modifiable by the program, and retain their last-stored values between program startup and program termination.
The C++ standard does not have a similar paragraph, but the same is implicit with no rule to the contrary.
Note that C++ and C are different languages and you should just choose one to ask your question about.
Upvotes: 41
Reputation: 16607
You can either pass them as parameters, or store them in global variables
. As long as you don't return from main
and try to process them in an atexit
handler or the destructor of an variable at global scope, they still exist and will be fine to access from any scope.
Upvotes: 3
Reputation: 103
yes, it is safe for ether C or C++, because there no thread after main was finish.
Upvotes: 2
Reputation: 134376
is it safe to use the argv pointer globally
This requires a little more clarification. As the C11
spec says in chapter §5.1.2.2.1, Program startup
[..].. with two parameters (referred to here as
argc
andargv
, though any names may be used, as they are local to the function in which they are declared)
That means, the variables themselves have a scope limited to main()
. They are not global themselves.
Again the standard says,
The parameters
argc
andargv
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.
That means, the lifetime of these variables are till main()
finishes execution.
So, if you're using a global variable to hold the value from main()
, you can safely use those globals to access the same in any other function(s).
Upvotes: 8