Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #108131
| Subject | Re: main() |
|---|---|
| Newsgroups | comp.lang.c |
| References | <ecUHA.285656$15.165152@fx07.am4> <ocqkob$jrl$1@dont-email.me> |
| From | bartc <bc@freeuk.com> |
| Message-ID | <nm5IA.358993$9O7.177093@fx22.am4> (permalink) |
| Organization | virginmedia.com |
| Date | 2017-04-14 15:52 +0100 |
On 14/04/2017 15:04, Lew Pitcher wrote: > bartc wrote: > >> I think this is not strictly part of the language but someone here may >> happen to know how it's done. >> >> The arguments to a programs entry point such as main(int n, char** args) >> are not set up by the OS, but by some function the C implementation >> arranges. >> >> On Windows at least, where such a function might use GetCommandLine() or >> __getmainargs() to obtain the command-line arguments and pass them to >> main(). >> >> What's the equivalent on Linux? Or will those args just magically be on >> the stack anyway? (Because after all Linux and C are very.. close.) > > In Linux, as in all Unix-like operating systems, the arguments ultimately > passed to main() as argv come from the invocation of one of the exec() > system functions. > > Those functions accept (in one form or another) > - a pointer to string that provides a filesystem path to the executable, and > - a list of pointers to strings, that each provide a value for the > corresponding argv[] element (including argv[0]). > > The OS ensures that the strings pointed to by the exec() call are copied to > known, standard locations in the memory map of the new process, and builds > the corresponding argc and argv[] arrays /before/ loading and invoking the > program code and C runtime startup. > > So, yes, those args "just magically" appear, as far as the new process is > concerned, just as the code and data of that new process "just magically" > appear in memory. I said is was magical because the order of those parameters seems to match the parameters as used in C's main() function. Either it's an amazing coincidence or one came first and the other copied. That may or may not been a sensible thing to do, but it makes it harder to disentangle what is C, and what is OS. -- bartc
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
main() bartc <bc@freeuk.com> - 2017-04-14 00:53 +0100
Re: main() Robert Wessel <robertwessel2@yahoo.com> - 2017-04-13 19:10 -0500
Re: main() antispam@math.uni.wroc.pl - 2017-04-14 02:51 +0000
Re: main() scott@slp53.sl.home (Scott Lurndal) - 2017-04-14 12:58 +0000
Re: main() bartc <bc@freeuk.com> - 2017-04-14 14:11 +0100
Re: main() Malcolm McLean <malcolm.mclean5@btinternet.com> - 2017-04-15 07:53 -0700
Re: main() supercat@casperkitty.com - 2017-04-15 08:43 -0700
Re: main() Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-04-15 16:50 +0100
Re: main() Keith Thompson <kst-u@mib.org> - 2017-04-15 15:31 -0700
Re: main() gordonb.y2etp@burditt.org (Gordon Burditt) - 2017-04-16 00:19 -0500
The exec*() family (Was: main()) gazelle@shell.xmission.com (Kenny McCormack) - 2017-04-15 17:01 +0000
Re: main() Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2017-04-14 10:04 -0400
Re: main() bartc <bc@freeuk.com> - 2017-04-14 15:52 +0100
Re: main() scott@slp53.sl.home (Scott Lurndal) - 2017-04-14 15:00 +0000
Re: main() Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2017-04-14 13:49 -0400
csiph-web