storage for argv

J

junky_fellow

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

Barry Schwarz

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

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


<<Remove the del for email>>
 
J

Joe Wright

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

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()'.
 
D

Dave Thompson

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top