Where does the OS store the command line arguments?
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. Wh开发者_Python百科y is that?
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.
I am working on Linux. In which section of memory are the command line arguments stored (stack or heap)?
That's up to the implementation; check your compiler documentation. All that's required is that that argc
and argv
, as well as the strings that the argv
array points to, be modifiable by the program.
I tried to execute free(argv)
and I got a segmentation fault. Why is that?
You didn't allocated argv
with malloc
. You don't need to free the command line argument vectory.
精彩评论