Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it required to free (or delete) the arguments passed into main in C?

Tags:

c++

c

If I have a main function like:

int main(int argc, char* argv[])
{
    return 0;
}

Is it required to deallocate argv? Can we deallocate argv? Should we deallocate argv?

like image 703
Matthew Avatar asked Jan 05 '16 19:01

Matthew


People also ask

Can we pass arguments in main () in C?

Yes, we can give arguments in the main() function. Command line arguments in C are specified after the name of the program in the system's command line, and these argument values are passed on to your program during program execution. The argc and argv are the two arguments that can pass to main function.

What are main arguments in C?

The main() function has two arguments that traditionally are called argc and argv and return a signed integer. Most Unix environments expect programs to return 0 (zero) on success and -1 (negative one) on failure. The argument vector, argv, is a tokenized representation of the command line that invoked your program.

Can I modify argv in C?

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. Save this answer.

What is argc and argv in main function?

The first parameter, argc (argument count) is an integer that indicates how many arguments were entered on the command line when the program was started. The second parameter, argv (argument vector), is an array of pointers to arrays of character objects.


2 Answers

Is it required to deallocate argv?

No.

Can we deallocate argv?

No. Though syntactically accepted, this would cause undefined behavior (runtime errors probably). The memory management of the argv array of pointers obliges the operating system (ABI bindings), which manages the process entry point (main()) and not the main() client definition.

Should we deallocate argv?

No, you didn't allocate it, so you don't need to deallocate.

like image 90
πάντα ῥεῖ Avatar answered Nov 15 '22 06:11

πάντα ῥεῖ


Simple Concept: If you didn't allocate, you need not free too. : ))

like image 36
Jay Avatar answered Nov 15 '22 05:11

Jay