storage for argv

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • junky_fellow@yahoo.co.in

    #1

    storage for argv

    Where does the storage is allocated for the various argument
    strings (char *argv[]) passed to the main() ?
    In main() we never allocate space for these. So, which function
    does it and how does it know how much space to allocate ?

    Thanx for any help ...

  • Barry Schwarz

    #2
    Re: storage for argv

    On 7 Jul 2005 21:24:45 -0700, junky_fellow@ya hoo.co.in wrote:
    [color=blue]
    >Where does the storage is allocated for the various argument
    >strings (char *argv[]) passed to the main() ?
    >In main() we never allocate space for these. So, which function
    >does it and how does it know how much space to allocate ?
    >
    >Thanx for any help ...[/color]

    It depends on your system. On my system, there is a startup function
    that creates the "environmen t" main() will run in but you probably
    weren't asking about an IBM mainframe.


    <<Remove the del for email>>

    Comment

    • Joe Wright

      #3
      Re: storage for argv

      junky_fellow@ya hoo.co.in wrote:[color=blue]
      > Where does the storage is allocated for the various argument
      > strings (char *argv[]) passed to the main() ?
      > In main() we never allocate space for these. So, which function
      > does it and how does it know how much space to allocate ?
      >
      > Thanx for any help ...
      >[/color]

      It's none of your business! :-)
      Seriously, it isn't. When your program is loaded by the OS, the CRT (C
      Run Time) for your program gets executed. It is the CRT that creates the
      sandbox that your program will play in. That includes building the input
      data structure 'char argv[]'. Only then does CRT call your 'main()'.

      --
      Joe Wright
      "Everything should be made as simple as possible, but not simpler."
      --- Albert Einstein ---

      Comment

      • Dave Thompson

        #4
        Re: storage for argv

        On 7 Jul 2005 21:24:45 -0700, junky_fellow@ya hoo.co.in wrote:
        [color=blue]
        > Where does the storage is allocated for the various argument
        > strings (char *argv[]) passed to the main() ?
        > In main() we never allocate space for these. So, which function
        > does it and how does it know how much space to allocate ?
        >[/color]
        It's up to the implementation; the only requirement in the Standard is
        that they be writable (within their supplied lengths). Note this is
        the string contents argv[*][*]; the string pointers argv[*] aren't
        guaranteed writable, although they usually are. The top pointer argv
        is a parameter of main() and thus writable if not declared const,
        which I think it can be (they're certainly compatible for functions in
        general, although 5.1.2.2.1 doesn't actually say 'compatible').

        In most implementations the command-line data, either as a line or as
        tokens, is supplied by the operating system, so the C runtime has to
        get it, somehow, into some space, somewhere, before entering main().

        The traditional Unix implementation is for the OS to provide packed
        tokens at the base of the stack (where base = high address and top =
        low address because the stack grows downward).

        Implementations on odder OS'es or no OS will be different.

        - David.Thompson1 at worldnet.att.ne t

        Comment

        Working...