Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #108140

Re: main()

From Lew Pitcher <lew.pitcher@digitalfreehold.ca>
Newsgroups comp.lang.c
Subject Re: main()
Followup-To comp.lang.c
Date 2017-04-14 13:49 -0400
Organization The Pitcher Digital Freehold
Message-ID <ocr1u7$a3l$1@dont-email.me> (permalink)
References <ecUHA.285656$15.165152@fx07.am4> <ocqkob$jrl$1@dont-email.me> <nm5IA.358993$9O7.177093@fx22.am4>

Followups directed to: comp.lang.c

Show all headers | View raw


bartc wrote:

> 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

It's not a coincidence

> or one came first and the other copied.

And, that's not what happened either.

C was developed as the high-level language to implement Unix with. Unix was 
originally developed in assembly language, then rewritten in C. It is by 
design that the C language, the C runtime support, the Unix runtime support, 
the Unix OS system calls, and the underlying Unix OS all work together to 
"magically" place arguments seeded by an exec() syscall into known locations 
in memory so that the executable initiated by the exec() syscall can locate 
them and populate the C main() argc and argv (and envp) parameters.

> 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.

I guess so, but only in that those OS that do not provide the requisite 
features natively must do so by emulation.

-- 
Lew Pitcher
"In Skills, We Trust"
PGP public key available upon request

Back to comp.lang.c | Previous | Next — Previous in thread | Find similar | Unroll thread


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