哪里C / C ++ main函数的参数? [英] Where are C/C++ main function's parameters?

查看:90
本文介绍了哪里C / C ++ main函数的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C / C ++,主要功能参数接收其类型的的char *

In C/C++, the main function receives parameters which is of type char*.

int main(int argc, char* argv[]){
  return 0;
}

的argv 的char *数组,它们指向字符串。在哪里这些字符串定位?他们是在堆上,或堆栈,或其他地方?

argv is array of char*, they point to strings. Where do these string locate? Are they on heap, or stack, or somewhere else?

推荐答案

它实际上是依赖编译器和操作系统的依赖的组合。 的main()是一个函数,就像任何其他的C函数,所以这两个参数的位置 ARGC 的argv 将遵循平台上的编译器的标准。例如对于大多数C编译器瞄准的x86他们将堆正上方的返回地址和保存的基指针(堆栈增长向下,记不清了)上。在x86_64参数在寄存器中传递,让 ARGC 将在%EDI argv的将在%RSI 。 code由编译器生成然后复制他们到堆栈中的主要功能,而这正是后来引用指向。这是为了让寄存器可用于函数调用从

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.

的char *的那ARGV点和实际字符序列块取值可能在任何地方。它们将在某些操作系统定义的位置开始,并可能由$ P $对同步码code。该链接器生成到堆栈或别的地方被复制。你必须看code为执行exec()和汇编pre-缓行链接器生成找出来。

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 ++ main函数的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆