Reputation: 354
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?
Upvotes: 1
Views: 769
Reputation: 12718
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.
Upvotes: 0
Reputation: 123596
The language definition itself only says this:
5.1.2.2.1 Program startupC 2011 Online Draft
...
2 If they are declared, the parameters to the main function shall obey the following constraints:
— The value ofargc
shall be nonnegative.
—argv[argc]
shall be a null pointer.
— If the value ofargc
is greater than zero, the array membersargv[0]
throughargv[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 ofargc
is greater than zero, the string pointed to byargv[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 ofargc
is greater than one, the strings pointed to byargv[1]
throughargv[argc-1]
represent the program parameters.
— The parametersargc
andargv
and the strings pointed to by theargv
array shall be modifiable by the program, and retain their last-stored values between program startup and program termination.
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.
Upvotes: 2
Reputation: 43337
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.
Upvotes: 5