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?
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).
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With