Where do command line arguments reside?
It is known the prototype for main funcion in C is int main(int argc, char **argv)
. Where do those strings pointed by argv array reside? Which memory segment are they in? Data, stack or heap?
Thanks.
Under Linux they are on the stack when the program starts, both the pointers themselves and the strings they point to. This will be somewhere up above main()
's stack frame. The C library startup code takes care of passing an appropriate pointer to main()
.
You can find the kernel code that sets up a new program's stack, including arguments and everything else, in fs/binfmt_elf.c
, in the function create_elf_tables()
.
(Fun fact I just learned: further up on the stack you can find 16 random bytes, placed there at exec
time by the kernel, for your RNG-seeding convenience. Just in case you don't want to go to the trouble of opening /dev/urandom
.)
上一篇: 全局(静态编译)变量位于何处?
下一篇: 命令行参数在哪里?