regarding command line arguments - C
This is a discussion on regarding command line arguments - C ; Hi All,
Can somebody help me by answering this question?
This is regarding the command line arguments that are passed to the
main() in C.
For the commands line parameters that are passed by the user, will the
memory for ...
-
regarding command line arguments
Hi All,
Can somebody help me by answering this question?
This is regarding the command line arguments that are passed to the
main() in C.
For the commands line parameters that are passed by the user, will the
memory for them be allocated on the stack or heap?
For instance, Consider the below code snippet that prints the command
line arguments:
int main(int argc, char *argv[])
{
int x;
printf("%d\n",argc);
for (x=0; x<argc; x++)
printf("%s\n",argv[x]);
return 0;
}
If I execute this program:
testPrg arg1 arg2 arg3
Then the memory for the command line arguments (arg1, arg2, arg3) be
allocated on the program stack or the heap memory.
Thanks in advance.
Warm Regards.
--
comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
-
Re: regarding command line arguments
sk.smawsk@gmail.com writes:
> This is regarding the command line arguments that are passed to the
> main() in C.
> For the commands line parameters that are passed by the user, will the
> memory for them be allocated on the stack or heap?
[...]
The standard doesn't specify. They're allocated wherever the calling
environment chooses to allocate them.
Note that the C standard doesn't mention either "stack" or "heap".
Why do you care? I can't think of anything you could do with an
answer to your question, even if a general answer were possible.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
--
comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
-
Re: regarding command line arguments
On 29 Mar 2007 01:26:17 GMT, sk.smawsk@gmail.com wrote in
comp.lang.c.moderated:
> Hi All,
>
> Can somebody help me by answering this question?
> This is regarding the command line arguments that are passed to the
> main() in C.
> For the commands line parameters that are passed by the user, will the
> memory for them be allocated on the stack or heap?
The C language does not define either "stack" or "heap". The start up
code provided by your implementation, the code that eventually calls
the main() function in your program, will build the array of pointers
to char somewhere. It may not be part of either of the areas that you
are thinking of when you use the terms "stack" or "heap".
The C standard does not know, or care, how the implementation manages
these details. Merely that the number of arguments, and a pointer to
an array of character pointers are received by main() as defined by
the standard.
> For instance, Consider the below code snippet that prints the command
> line arguments:
>
> int main(int argc, char *argv[])
> {
> int x;
>
> printf("%d\n",argc);
> for (x=0; x<argc; x++)
> printf("%s\n",argv[x]);
> return 0;
> }
>
> If I execute this program:
>
> testPrg arg1 arg2 arg3
>
> Then the memory for the command line arguments (arg1, arg2, arg3) be
> allocated on the program stack or the heap memory.
There are two points to make here:
1. If you are interested in the mechanism of a particular compiler,
you should ask in a compiler-specific group.
2. Why do you care? As long as main() is called with the proper
parameters, where the implementation actually places the strings, or
how it allocates whatever memory it might need to do so, cannot have
any possible impact on a valid, strictly conforming C program.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
--
comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
-
Re: regarding command line arguments
On 29 Mar 2007 01:26:17 GMT, sk.smawsk@gmail.com wrote:
>Hi All,
>
>Can somebody help me by answering this question?
>This is regarding the command line arguments that are passed to the
>main() in C.
>For the commands line parameters that are passed by the user, will the
>memory for them be allocated on the stack or heap?
C doesn't define a stack or a heap. My system has neither. Your
system may have one or both. You need to ask in a newsgroup that
deals with your compiler on your operating system.
But the real question is why do you care. In particular, your code
should not depend on the answer.
Remove del for email
--
comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
-
Re: regarding command line arguments
sk.smawsk@gmail.com wrote:
> This is regarding the command line arguments that are passed to the
> main() in C. For the commands line parameters that are passed by
> the user, will the memory for them be allocated on the stack or heap?
That is implementation-defined. You shouldn't care about it, since you also
shouldn't deallocate them in any way. In short, the memory doesn't belong
to you. Now, if you are interested how main() is called, you could take a
look at some compiler's sources, but even then, it shouldn't make a
difference to you.
Uli
--
comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
-
Re: regarding command line arguments
sk.smawsk@gmail.com wrote:
[...]
> For the commands line parameters that are passed by the user, will the
> memory for them be allocated on the stack or heap?
The answer to your question is, I'm afraid, yes. Or possibly no.
[Disclaimer: my copy of the standard isn't around. This is from memory. I
could be wrong.]
Exactly what argv points at isn't defined. It's only defined that they point
at *something*. Typically they're pushed onto the stack by the operating
system before the process starts, but that isn't always the case --- they may
point into ROM, or the CRT may have gotten them from the OS via a syscall and
parsed them into a heap structure, etc.
In fact, it shouldn't matter where they are --- there's nothing you're allowed
to do to them where it matters. (You're allowed to modify argv itself, but you
certainly can't touch the strings that argv[] points at.)
--
┌── dg@cowlark.com ─── http://www.cowlark.com ───────────────────
│ "Thou who might be our Father, who perhaps may be in Heaven, hallowed be
│ Thy Name, if Name Thou hast and any desire to see it hallowed..." ---
│ _Creatures of Light and Darkness_
--
comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
-
Re: regarding command line arguments
sk.smawsk@gmail.com wrote:
> For the commands line parameters that are passed by the user, will the
> memory for them be allocated on the stack or heap?
Not necessarily either (e.g. PDP-11 Unix).
The only way it could matter is if an atexit-registered handler
uses a copy of a pointer to an argument (passed to it via a global),
e.g. saving argv[0] to use as a program name for printing a
termination message like "sort: cleaning up"). Since the lifetime
of the argument has expired once main returns, that would be invalid
usage; if the data is needed at that stage, it must be saved in a
copy that will still be "live" (static or dynamically allocated).
--
comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
-
Re: regarding command line arguments
sk.smawsk@gmail.com wrote:
> For the commands line parameters that are passed by the user, will the
> memory for them be allocated on the stack or heap?
Actually that's none of your business to know. They exist as long as
you need them, period.
And that's before we consider that there's not even any guarantee that
such a thing as a "stack" even exists, in Standard C.
--
comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
-
Re: regarding command line arguments
sk.smawsk@gmail.com wrote:
> Hi All,
>
> Can somebody help me by answering this question?
> This is regarding the command line arguments that are passed to the
> main() in C.
> For the commands line parameters that are passed by the user, will the
> memory for them be allocated on the stack or heap?
Not necessarily.
--
comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
-
Re: regarding command line arguments
>Can somebody help me by answering this question?
>This is regarding the command line arguments that are passed to the
>main() in C.
>For the commands line parameters that are passed by the user, will the
>memory for them be allocated on the stack or heap?
Possibly not.
The C standard doesn't define these terms, so I'm using these
definitions:
Stack: That place from which memory allocated by malloc() and friends
comes from. (e.g. Motorola, Samsung, or Texas Instruments)
Heap: That place from which memory for automatic variables comes from.
Nothing in these definitions prohibit both from taking memory from the
SAME place.
You must not free() the command-line arguments; the penalty is undefined
behavior. Also, the command-line arguments don't vanish when a function
returns. NEITHER of these places seems appropriate.
>For instance, Consider the below code snippet that prints the command
>line arguments:
>
>int main(int argc, char *argv[])
>{
> int x;
>
> printf("%d\n",argc);
> for (x=0; x<argc; x++)
> printf("%s\n",argv[x]);
> return 0;
>}
>
>If I execute this program:
>
>testPrg arg1 arg2 arg3
>
>Then the memory for the command line arguments (arg1, arg2, arg3) be
>allocated on the program stack or the heap memory.
Not necessarily.
--
comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Similar Threads
-
By Application Development in forum DOTNET
Replies: 3
Last Post: 12-11-2007, 06:38 AM
-
By Application Development in forum Fortran
Replies: 4
Last Post: 12-07-2007, 07:54 AM
-
By Application Development in forum DOTNET
Replies: 1
Last Post: 04-10-2007, 09:51 AM
-
By Application Development in forum DOTNET
Replies: 0
Last Post: 02-01-2007, 06:10 PM
-
By Application Development in forum C
Replies: 15
Last Post: 02-16-2004, 02:35 PM