Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does the OS store the command line arguments?

Tags:

c

I am working on Linux. In which section of memory are the command line arguments stored (stack or heap)?

I tried to execute free(argv) and I got a segmentation fault. Why is that?

like image 484
Bruce Avatar asked Oct 11 '11 15:10

Bruce


People also ask

Where is command line stored?

The quickest way to open a Command Prompt window is through the Power User Menu, which you can access by right-clicking the Windows icon in the bottom-left corner of your screen, or with the keyboard shortcut Windows Key + X. It'll appear in the menu twice: Command Prompt and Command Prompt (Admin).

What is command line argument in OS?

Command line arguments are the arguments which the user gives from the operating system's command line during the time of execution. Earlier, we used main() functions without arguments. These command line arguments are handled by the main() function.

Are command line arguments stored as strings?

Python stores all command-line arguments as strings because all command-line arguments are given to Python as text by the operating system. Python doesn't do any processing with them; it's up to us to make meaning of these arguments.


1 Answers

I tried to execute free(argv) and I got a segmentation fault. Why is that?

You can only free what you malloc/calloc (and possibly realloc later on). Trying to free something else invokes Undefined Behaviour. One (good) way UB manifests itself is by producing a segmentation fault; a (bad) way is to make the program appear to work as intended.

As to where they are ... read section 5.1.2.2.1 of the C99 Standard -- its unspecified.

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.

like image 66
pmg Avatar answered Nov 05 '22 12:11

pmg