Where are C/C++ main function's parameters?

前端 未结 10 1792
清酒与你
清酒与你 2020-11-29 02:31

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

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

相关标签:
10条回答
  • 2020-11-29 02:37

    These parameters are no different than any other function's parameters. If the architecture's calling sequence requires parameters to go through stack they are on stack. If, like on, x86-64 some parameters go in registers these also go in registers.

    0 讨论(0)
  • 2020-11-29 02:38

    The answer to this question is compiler-dependent. This means it is not treated in the C standard, so anyone can implement that as he or she would like to. This is normal since also operating systems don't have a common accepted, standard way to start processes and finish them.

    Let's imagine a simple, why-not scenario.

    The process receives by some mechanism the arguments written in the command line. argc is then just an int which is pushed to the stack by the bootstrap function the compiler put as the entry point for the process of the program (part of the runtime). The actual values are get from the operating system, and can be, say, written in a memory block of the Heap. Then the argv vector is built and the address to its first position also pushed into the stack.

    Then the function main(), which must be provided by the programmer, is called, and its return value is saved for later (nearly inmediate) use. The structures in the Heap are freed, and the exit code obtained for main is exported to the operating system. The process finishes.

    0 讨论(0)
  • 2020-11-29 02:48

    While you are able to access to the actual parameters, I think their actual location does not matter at all.

    0 讨论(0)
  • 2020-11-29 02:49

    They are compiler magic, and implementation-dependent.

    0 讨论(0)
  • 2020-11-29 02:51

    Here's what the C standard (n1256) says:

    5.1.2.2.1 Program startup
    ...
    2 If they are declared, the parameters to the main function shall obey the following constraints:

    • The value of argc shall be nonnegative.

    • argv[argc] shall be a null pointer.

    • If the value of argc is greater than zero, the array members argv[0] through argv[argc-1] inclusive shall contain pointers to strings, which are given implementation-defined values by the host environment prior to program startup. The intent is to supply to the program information determined prior to program startup from elsewhere in the hosted environment. If the host environment is not capable of supplying strings with letters in both uppercase and lowercase, the implementation shall ensure that the strings are received in lowercase.

    • If the value of argc is greater than zero, the string pointed to by argv[0] represents the program name; argv[0][0] shall be the null character if the program name is not available from the host environment. If the value of argc is greater than one, the strings pointed to by argv[1] through argv[argc-1] represent the program parameters.

    • The parameters argc and argv and 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.

    The last bullet is the most interesting wrt where the string values are stored. It doesn't specify heap or stack, but it does require that the strings be writable and have static extent, which places some limits on where the string contents may be located. As others have said, the exact details will depend on the implementation.

    0 讨论(0)
  • 2020-11-29 02:52

    The argument list is part of the process environment, similar to (but distinct from) environment variables.

    0 讨论(0)
提交回复
热议问题