 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Frank Birbacher Guest
|
Posted: Mon Oct 25, 2004 7:51 pm Post subject: layout of argv[] |
|
|
Hi!
Does the standard define the memory layout of the second
parameter to main? More concrete: Is the following program legal?
#include <iostream>
#include <iterator>
#include <algorithm>
#include <cstdlib>
int main(const int argc, const char* argv[])
{
const char* const end =
argv[argc-1] + strlen(argv[argc-1]);
std::copy(argv[0], end,
std::ostream_iterator<char>(std::cout));
}
It assumes the strings in argv are in contiguous memory.
Is a potential third parameter similar in this respect?
Frank
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Tue Oct 26, 2004 1:27 pm Post subject: Re: layout of argv[] |
|
|
Frank Birbacher wrote:
| Quote: | Hi!
Does the standard define the memory layout of the second
parameter to main? More concrete: Is the following program legal?
int main(const int argc, const char* argv[])
|
Whether the implementaiton supports this or not is implementation-defined.
This is NOT one of the two guaranteed to be supported signatures. The
second argument should not have const in it.
| Quote: | {
const char* const end =
argv[argc-1] + strlen(argv[argc-1]);
std::copy(argv[0], end,
std::ostream_iterator<char>(std::cout));
}
It assumes the strings in argv are in contiguous memory.
|
There's no such guarantee.
| Quote: |
Is a potential third parameter similar in this respect?
|
What third parameter.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Tue Oct 26, 2004 1:28 pm Post subject: Re: layout of argv[] |
|
|
Frank Birbacher wrote:
| Quote: | Does the standard define the memory layout of the second
parameter to main?
|
No.
| Quote: | More concrete: Is the following program legal?
#include <iostream
#include
#include
#include
int main(const int argc, const char* argv[])
{
const char* const end =
argv[argc-1] + strlen(argv[argc-1]);
std::copy(argv[0], end,
std::ostream_iterator
}
It assumes the strings in argv are in contiguous memory.
|
There is no requirement in the Standard that 'argv' strings are placed
in contiguous memory. Each element of the 'argv' array is its own NTMBS,
but there is no guarantee that they follow each other.
| Quote: | Is a potential third parameter similar in this respect?
|
Is that the third parameter (argument) of the 'main' function? It is not
defined by the Standard at all.
V
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Jack Klein Guest
|
Posted: Tue Oct 26, 2004 10:44 pm Post subject: Re: layout of argv[] |
|
|
On 25 Oct 2004 15:51:53 -0400, Frank Birbacher
<bloodymir.crap (AT) gmx (DOT) net> wrote in comp.lang.c++.moderated:
| Quote: | Hi!
Does the standard define the memory layout of the second
parameter to main? More concrete: Is the following program legal?
|
The standard does not define the memory layout. The pointers are
contiguous, the memory they point to is neither required nor
guaranteed to be.
I don't know what 'legal' means, but the behavior of the program is
not defined by the standard, so is automatically undefined.
| Quote: |
#include <iostream
#include
#include
#include
int main(const int argc, const char* argv[])
{
const char* const end =
argv[argc-1] + strlen(argv[argc-1]);
std::copy(argv[0], end,
std::ostream_iterator
}
It assumes the strings in argv are in contiguous memory.
|
That's the assumption that might work or might not. It is just plain
not defined.
| Quote: | Is a potential third parameter similar in this respect?
|
The C++ standard allows, but does not require, additional parameters
to main(). It does not define any such possible parameters at all, do
they behave as the implementation documents them to behave and there
is no language issue to the question.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
News Subsystem Guest
|
Posted: Tue Oct 26, 2004 10:45 pm Post subject: Re: layout of argv[] |
|
|
If I recall correctly, the pointers are in consecutive storage locations
(i.e. you can set a pointer = argv[0] and then increment it with ++pointer
but not necessarily the actual values. Some implmentations may have them in
continguous memory but I wouldn't rely on this for anything intended to be
portable.
Nathanael Hoyle
Remove the obvious from the email address to reply by email.
"Frank Birbacher" <bloodymir.crap (AT) gmx (DOT) net> wrote
| Quote: | Hi!
Does the standard define the memory layout of the second
parameter to main? More concrete: Is the following program legal?
#include <iostream
#include
#include
#include
int main(const int argc, const char* argv[])
{
const char* const end =
argv[argc-1] + strlen(argv[argc-1]);
std::copy(argv[0], end,
std::ostream_iterator
}
It assumes the strings in argv are in contiguous memory.
Is a potential third parameter similar in this respect?
Frank
|
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Richter Guest
|
Posted: Tue Oct 26, 2004 10:49 pm Post subject: Re: layout of argv[] |
|
|
Hi,
| Quote: | Does the standard define the memory layout of the second
parameter to main?
|
No.
| Quote: | More concrete: Is the following program legal?
int main(const int argc, const char* argv[])
{
const char* const end =
argv[argc-1] + strlen(argv[argc-1]);
std::copy(argv[0], end,
std::ostream_iterator<char>(std::cout));
}
|
No, the result of this program is undefined.
| Quote: | Is a potential third parameter similar in this respect?
|
It is similar in the sense that you get no guarantee about
the memory layout of the environment strings, yes.
So long,
Thomas
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Tue Oct 26, 2004 11:26 pm Post subject: Re: layout of argv[] |
|
|
Frank Birbacher <bloodymir.crap (AT) gmx (DOT) net> wrote
| Quote: | Does the standard define the memory layout of the second
parameter to main?
|
Yes. It must be a C-style array of char*. And the memory layout of
C-style arrays is very strictly defined. (I presume that this is what
you mean. Strictly speaking, the second parameter of main is a pointer
to a pointer, and the standard says nothing concerning what a pointer to
a pointer looks like in memory.)
| Quote: | More concrete: Is the following program legal?
#include <iostream
#include
#include
#include
int main(const int argc, const char* argv[])
{
const char* const end =
argv[argc-1] + strlen(argv[argc-1]);
std::copy(argv[0], end,
std::ostream_iterator
}
It assumes the strings in argv are in contiguous memory.
|
Which isn't required, and isn't even always the case. I think that it
is generally the case under Unix, but was not the case for most of the
non Unix systems I've used.
| Quote: | Is a potential third parameter similar in this respect?
|
Any potential third parameter is totally implementation defined. what
is guaranteed concerning it is guaranteed only by the implementation.
--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Frank Birbacher Guest
|
Posted: Wed Oct 27, 2004 12:04 am Post subject: Re: layout of argv[] |
|
|
Hi!
Ron Natalie wrote:
| Quote: | int main(const int argc, const char* argv[])
Whether the implementaiton supports this or not is implementation-defined.
This is NOT one of the two guaranteed to be supported signatures. The
second argument should not have const in it.
|
Ok, I see. You said the second argument should not have 'const'
in it. Well, would a top-level const be allowed? Like:
int main(const int argc, char* const argv[]) ?
| Quote: | What third parameter.
|
Hmm, as Victor Bazarov said, there is not third parameter
according to the standard. Well, where does it come from?
int main(int argc, char* argv[], char* env[])
This is accepted at least by gcc 3.3 in "-ansi -pedantic -W". The
third argument refers to environment variables.
Frank
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Randy Maddox Guest
|
Posted: Wed Oct 27, 2004 1:01 am Post subject: Re: layout of argv[] |
|
|
Frank Birbacher <bloodymir.crap (AT) gmx (DOT) net> wrote
| Quote: | Hi!
Does the standard define the memory layout of the second
parameter to main? More concrete: Is the following program legal?
#include <iostream
#include
#include
#include
int main(const int argc, const char* argv[])
{
const char* const end =
argv[argc-1] + strlen(argv[argc-1]);
std::copy(argv[0], end,
std::ostream_iterator
}
It assumes the strings in argv are in contiguous memory.
Is a potential third parameter similar in this respect?
Frank
|
Both argc and argv are discussed in 3.6.1, Main Function, but nowhere
there, or anywhere else that I could find, does the Standard say
anything about the layout of the strings pointed to by the pointers in
argv.
The layout of those strings in contiguous memory, separated only by
their null terminators, is certainly easy and efficient and therefore
probably likely, but there is no reason to assume it is universal.
There is a note at the end of 3.6.1/2 that recommends that any
optional additional parameters to main() be added after argv.
Randy.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Wed Oct 27, 2004 1:54 pm Post subject: Re: layout of argv[] |
|
|
Frank Birbacher <bloodymir.crap (AT) gmx (DOT) net> wrote
| Quote: | Ron Natalie wrote:
int main(const int argc, const char* argv[])
Whether the implementaiton supports this or not is
implementation-defined. This is NOT one of the two guaranteed to be
supported signatures. The second argument should not have const in
it.
Ok, I see. You said the second argument should not have 'const' in
it. Well, would a top-level const be allowed? Like:
int main(const int argc, char* const argv[]) ?
|
That's still not a top level const. That's the equivalent of:
int main( int const, char *const * argv )
Something like:
int main( int const, char **const argv )
would be allowed, but I don't see any advantage in it.
| Quote: | What third parameter.
Hmm, as Victor Bazarov said, there is not third parameter according to
the standard. Well, where does it come from?
int main(int argc, char* argv[], char* env[])
This is accepted at least by gcc 3.3 in "-ansi -pedantic -W". The
third argument refers to environment variables.
|
It's a common extension. I think it is even required that Posix
compliant implementations support it (but I'm not sure).
The standard allows any number of additional signatures to work, as long
as the return value is int.
--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Michiel Salters Guest
|
Posted: Thu Oct 28, 2004 1:45 pm Post subject: Re: layout of argv[] |
|
|
[email]rmaddox (AT) isicns (DOT) com[/email] (Randy Maddox) wrote in message news:<8c8b368d.0410260848.24731649 (AT) posting (DOT) google.com>...
| Quote: | Both argc and argv are discussed in 3.6.1, Main Function, but nowhere
there, or anywhere else that I could find, does the Standard say
anything about the layout of the strings pointed to by the pointers in
argv.
The layout of those strings in contiguous memory, separated only by
their null terminators, is certainly easy and efficient and therefore
probably likely, but there is no reason to assume it is universal.
|
It would be unlikely when the program is started by another program,
and that program provided the char*[].
Regards,
Michiel Salters
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Thu Oct 28, 2004 2:00 pm Post subject: Re: layout of argv[] |
|
|
Frank Birbacher wrote:
| Quote: | Hi!
Ok, I see. You said the second argument should not have 'const'
in it. Well, would a top-level const be allowed? Like:
int main(const int argc, char* const argv[]) ?
|
Yes, const on the parameters themselves do not affect the function
signature. The only signatures that are guaranteed to be supported
are:
int main()
int main(int, char*[]); //which is what the standard says,but
// really the signature is char**.
| Quote: | Hmm, as Victor Bazarov said, there is not third parameter
according to the standard. Well, where does it come from?
int main(int argc, char* argv[], char* env[])
|
This is a UNIX "implementation" issue. It's not defined by C++
other than C++ says it's an allowed extension.
| Quote: |
This is accepted at least by gcc 3.3 in "-ansi -pedantic -W". The
third argument refers to environment variables.
The compiler can accept any alternate signatures of main that it |
wants. "Implementation defined."
Using the third argument env paramter is not portable, however using
getenv is.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Maxim Yegorushkin Guest
|
Posted: Fri Oct 29, 2004 2:15 am Post subject: Re: layout of argv[] |
|
|
<kanze (AT) gabi-soft (DOT) fr> wrote:
| Quote: | Ok, I see. You said the second argument should not have 'const' in
it. Well, would a top-level const be allowed? Like:
int main(const int argc, char* const argv[]) ?
That's still not a top level const. That's the equivalent of:
int main( int const, char *const * argv )
Something like:
int main( int const, char **const argv )
would be allowed, but I don't see any advantage in it.
|
At least some compilers allow this:
int main(int, char const* const*);
Is it portable?
--
Maxim Yegorushkin
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Fri Oct 29, 2004 2:26 pm Post subject: Re: layout of argv[] |
|
|
"Maxim Yegorushkin" <e-maxim (AT) yandex (DOT) ru> wrote
| Quote: | kanze (AT) gabi-soft (DOT) fr> wrote:
Ok, I see. You said the second argument should not have 'const' in
it. Well, would a top-level const be allowed? Like:
int main(const int argc, char* const argv[]) ?
That's still not a top level const. That's the equivalent of:
int main( int const, char *const * argv )
Something like:
int main( int const, char **const argv )
would be allowed, but I don't see any advantage in it.
At least some compilers allow this:
int main(int, char const* const*);
Is it portable?
|
Not according to the standard.
I think that a lot of compilers simply don't verify anything concerning
the parameter types of main -- both Sun CC and g++ accept int main(
double, int* ), for example. On execution, the result is undefined
behavior. In the case you give, the undefined behavior will almost
certainly be that the code works as expected, since char const* const*
will have the same representation as the char** which the start off
routine passed.
On the other hand, I think that a compiler IS allowed to refuse to
compile a definition which corresponds to something it doesn't support.
--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Fri Oct 29, 2004 2:34 pm Post subject: Re: layout of argv[] |
|
|
Maxim Yegorushkin wrote:
| Quote: |
At least some compilers allow this:
int main(int, char const* const*);
Is it portable?
|
No. int main() and int main(int char*[]) are the only
two signatures that are guaranteed.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|