在C/C++中,main函数接收char*
类型的参数.
In C/C++, the main function receives parameters which are of type char*
.
int main(int argc, char* argv[]){
return 0;
}
argv
是一个 char*
数组,指向字符串.这些字符串位于何处?它们是在堆、栈还是其他地方?
argv
is an array of char*
, and points to strings. Where are these string located? Are they on the heap, stack, or somewhere else?
其实就是编译器依赖和操作系统依赖的结合.main()
和其他任何 C 函数一样是一个函数,所以 argc
和 argv
两个参数的位置将遵循编译器的标准平台上.例如对于大多数面向 x86 的 C 编译器,它们将位于返回地址和保存的基指针上方的堆栈上(记住,堆栈向下增长).在 x86_64 上,参数在寄存器中传递,因此 argc
将在 %edi
中,argv
将在 %rsi
中.编译器生成的 main 函数中的代码然后将它们复制到堆栈中,这就是后面的引用指向的地方.这样寄存器就可以用于来自 main
的函数调用.
It's actually a combination of compiler dependence and operating system dependence. main()
is a function just like any other C function, so the location of the two parameters argc
and argv
will follow standard for the compiler on the platform. e.g. for most C compilers targeting x86 they will be on the stack just above the return address and the saved base pointer (the stack grows downwards, remember). On x86_64 parameters are passed in registers, so argc
will be in %edi
and argv
will be in %rsi
. Code in the main function generated by the compiler then copies them to the stack, and that is where later references point. This is so the registers can be used for function calls from main
.
argv 指向的 char*
块和实际的字符序列可以在任何地方.它们将从某个操作系统定义的位置开始,并且可能会被链接器生成的前导码复制到堆栈或其他地方.您必须查看 exec()
的代码和链接器生成的汇编程序前导码才能找出答案.
The block of char*
s that argv points to and the actual sequences of characters could be anywhere. They will start in some operating system defined location and may be copied by the pre-amble code that the linker generates to the stack or somewhere else. You'll have to look at the code for exec()
and the assembler pre-amble generated by the linker to find out.
这篇关于C/C++ 主函数的参数在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!