简体   繁体   中英

How does a C program take command line arguments at run time?

I know that C programs can be passed arguments externally (from the command line) like

gcc.exe -std=c++17 -o fileName 

by declaring function main as taking parameters

int main(int argc, char* argv[])

My question is, where are these strings stored? In C, isn't the size of an array supposed to be known at compile-time. You could pass many, many arguments (strings) to the program, or none at all. But the program has already been compiled into an executable - it can't take more or less memory than it already does. Or is this memory allocated dymamically?

On DOS, the strings are stored at __psp:128 (__psp is usually DS but need not be).

On Windows, the strings are stored on the heap. The original command line is stored in a memory slab with a really low address.

On Linux, the strings are stored on the bottom of the stack.

The language definition itself only says this:

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.
C 2011 Online Draft

Which is basically a long-winded way of saying - it's up to the implementation. The strings come from the host environment, they're stored in such a way that they are modifiable, and they exist over the lifetime of the program. They're set up as the program is loaded into memory, in many cases probably taking a chunk of stack space.

In general, in unix operating systems, the values for the strings that compose the array argv and the array of pointers, and the value for argc are initializing the first stack frame in the stack. Just above the first return value, to make main() to work as a normal C function. This is handled normally by the exec(2) system call, in the majority of unices (including linux, bsd systems, solaris, and possibly more)

The official answer, although is that this is implementation dependent, so you cannot assume anything about where or how they are stored in the program virtual space.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM