[ACCEPTED]-Where are C/C++ main function's parameters?-main

Accepted answer
Score: 31

They are compiler magic, and implementation-dependent.

0

Score: 30

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 8 bullet is the most interesting wrt where 7 the string values are stored. It doesn't 6 specify heap or stack, but it does require 5 that the strings be writable and have static 4 extent, which places some limits on where the 3 string contents may be located. As others 2 have said, the exact details will depend 1 on the implementation.

Score: 18

It's actually a combination of compiler 23 dependence and operating system dependence. main() is 22 a function just like any other C function, so 21 the location of the two parameters argc and 20 argv will follow standard for the compiler on 19 the platform. e.g. for most C compilers 18 targeting x86 they will be on the stack 17 just above the return address and the saved 16 base pointer (the stack grows downwards, remember). On 15 x86_64 parameters are passed in registers, so 14 argc will be in %edi and argv will be in %rsi. Code in the 13 main function generated by the compiler 12 then copies them to the stack, and that 11 is where later references point. This is 10 so the registers can be used for function 9 calls from main.

The block of char*s that argv points 8 to and the actual sequences of characters 7 could be anywhere. They will start in some 6 operating system defined location and may 5 be copied by the pre-amble code that the 4 linker generates to the stack or somewhere 3 else. You'll have to look at the code for 2 exec() and the assembler pre-amble generated by 1 the linker to find out.

Score: 8

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

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

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

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

Score: 3

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

Score: 3

As pmg mentions, when main is called recursively, it's 8 up to the caller where the arguments point 7 to. Basically the answer is the same on 6 the original invocation of main, except that 5 the "caller" is the C implementation/OS.

On 4 UNIX-y systems, the strings that argv points 3 to, the argv pointers themselves, and the process's 2 initial environment variables are almost 1 always stored at the very top of the stack.

Score: 3

As many other answers here point out, the 36 precise mechanism a compiler implementation 35 uses to pass arguments to main is unspecified 34 by the standard (as is the mechanism a compiler 33 uses to pass any arguments to a function). Strictly 32 speaking, the compiler need not even pass 31 anything useful in those parameters, since 30 the values are implementation-defined. But 29 neither of these are particularly helpful 28 answers.

The typical C (or C++) program is 27 compiled for what's known as a 'hosted' execution 26 environment (using function main() as the starting 25 point of your program is one of the requirements 24 for a hosted environment). The key thing 23 to know is that the compiler arranges things 22 so that when the executable is launched 21 by the operating system, the compiler's 20 runtime gets control initially - not the 19 main() function. The runtime's initialization 18 code performs whatever initialization is 17 necessary, including allocating memory for 16 the arguments to main(), then it transfers control 15 to main().

The memory for the arguments to main() could 14 come from the heap, could be allocated on 13 the stack (possibly using techniques that 12 aren't available to standard C code), or 11 could use statically allocated memory, though 10 that's a less likely option just because 9 it's less flexible. The standard does require 8 that the memory used for the strings pointed 7 to by argv are modifiable and that modifications 6 made to those string persist throughout 5 the program's lifetime.

Just be aware that 4 before execution reaches main(), quite a bit of 3 code has already been run that's setting 2 up the environment for your program to run 1 in.

Score: 2

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

Score: 2

Usually it is unknown where they are.

#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[]) {
  char **foo;
  char *bar[] = {"foo", "bar"};

  (void)argv; /* avoid unused argv warning */

  foo = malloc(sizeof *foo);
  foo[0] = malloc(42);
  strcpy(foo[0], "forty two");

  /* where is foo located? stack? heap? somewhere else? */
  if (argc != 42) main(42, foo); else return 0;

  /* where is bar located? stack? heap? somewhere else? */
  if (argc != 43) main(43, bar); else return 0;
  /* except for the fact that bar elements
  ** point to unmodifiable strings
  ** this call to main is perfectably reasonable */

  return 0;
  /* please ignore memory leaks, thank you */
}

0

More Related questions