Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

208
Views
Is it safe to use the argv pointer globally?

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;
}
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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

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.

over 4 years ago · Santiago Trujillo Report

0

It should be safe so long as main() function does not exit. A few examples of things that can happen after main() exits are:

  1. Destructors of global and static variables
  2. Threads running longer than 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.

over 4 years ago · Santiago Trujillo Report

0

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 and argv, 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 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.

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

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!