Topic: Calling main from main??


Author: jim.hyslop@leitch.com (Jim Hyslop)
Date: 1997/04/11
Raw View
In article <5iht1i$ovl@fs7.ece.cmu.edu>, dacut@henry.ece.cmu.edu
says...
> I certainly wouldn't expect &main to yield a consistent result across
> platforms or even compilers.  I would feel much better, however, if
> the C++ standard allowed something like:
>
> Warning yourprogram.cpp 8: You doofus; taking the address of main is a
> bad idea, and I'm going to reformat your hard disk.
>
> rather than
>
> Error yourprogram.cpp 8: Cannot take the address of main.
OK, many compilers support warning levels - how about abuse levels?
Abuse level 0:
Error yourprogarm.cpp 8: Cannot take the address of main.

Abuse level 1:
Duh!  You can't take the address of main.  Like, hello!

Abuse level 2:
Are you freakin' brain-dead or what?  Take the address of main.  What
a moron!

Abuse level 3:
Who the hell ever taught you to program?  You try taking the address
of main again and I'll ram your keyboard so far down your throat
you'll be able to type with your toes!

and so on.
[OK, now back to the topic...]

--
Jim Hyslop
jim.hyslop@leitch.com
Children have an innate understanding of statistics.  When they report
"Everyone is doing it," their findings are usually based on a survey
of one.
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1997/04/09
Raw View
dacut@henry.ece.cmu.edu (David A. Cuthbert) writes:

>Steve Clamage <Stephen.Clamage@eng.sun.com> wrote:
>>Like the ARM, the draft standard for C++ exlicitly disallows calling
>>or taking the address of main. C has different rules.
>
>Is there a reason why taking the address of main() is disallowed as
>opposed to implementation defined?

Making it implementation defined would reduce portability of C++ programs.

A more interesting (IMHO) question: why is it disallowed, rather than allowed?
I can think of some historical reasons, but I can't think of any reasons
that I would consider good from a language design perspective.

--
Fergus Henderson <fjh@cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3         |     -- the last words of T. S. Garp.
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: dacut@henry.ece.cmu.edu (David A. Cuthbert)
Date: 1997/04/10
Raw View
Fergus Henderson <fjh@mundook.cs.mu.OZ.AU> wrote:
>dacut@henry.ece.cmu.edu (David A. Cuthbert) writes:
>>Steve Clamage <Stephen.Clamage@eng.sun.com> wrote:
>>>Like the ARM, the draft standard for C++ exlicitly disallows calling
>>>or taking the address of main. C has different rules.

>>Is there a reason why taking the address of main() is disallowed as
>>opposed to implementation defined?

>Making it implementation defined would reduce portability of C++ programs.

Well, seeing as how I wanted to use it to do stack walkbacks, I wasn't
exactly looking for portability.  :-)

>A more interesting (IMHO) question: why is it disallowed, rather than allowed?
>I can think of some historical reasons, but I can't think of any reasons
>that I would consider good from a language design perspective.

What's the difference between allowed [but unspecified] and
implementation defined?

I certainly wouldn't expect &main to yield a consistent result across
platforms or even compilers.  I would feel much better, however, if
the C++ standard allowed something like:

Warning yourprogram.cpp 8: You doofus; taking the address of main is a
bad idea, and I'm going to reformat your hard disk.

rather than

Error yourprogram.cpp 8: Cannot take the address of main.
--
David A. Cuthbert (henry.ece.cmu.edu!dacut)
Graduate Student, Electrical and Computer Engineering
Data Storage Systems Center, Carnegie Mellon University
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: fjh@murlibobo.cs.mu.OZ.AU (Fergus Henderson)
Date: 1997/04/10
Raw View
dacut@henry.ece.cmu.edu (David A. Cuthbert) writes:

>Fergus Henderson <fjh@mundook.cs.mu.OZ.AU> wrote:
>>dacut@henry.ece.cmu.edu (David A. Cuthbert) writes:
>>>Is there a reason why taking the address of main() is disallowed as
>>>opposed to implementation defined?
>
>>Making it implementation defined would reduce portability of C++ programs.
>
>Well, seeing as how I wanted to use it to do stack walkbacks, I wasn't
>exactly looking for portability.  :-)

Yes, but everyone else is.  It would be unfortunate if a compiler vendor
decided to allow it, e.g. for cases like yours, and then people used
this feature without realizing that it is non-portable.

>>A more interesting (IMHO) question: why is it disallowed, rather than allowed?
>>I can think of some historical reasons, but I can't think of any reasons
>>that I would consider good from a language design perspective.
>
>What's the difference between allowed [but unspecified] and
>implementation defined?

The difference is that with implementation defined behaviour, the
implementation is required to document the behaviour.

>I certainly wouldn't expect &main to yield a consistent result across
>platforms or even compilers.

It does do so in C: the result is consistently a pointer to the
function `main'.  Now the value that you get if you cast that pointer
to `int' or if you print it out with `printf("%p", &main)' or
if you do anything similar is unspecified or implementation-defined,
but that is a separate issue; if you just dereference it, e.g.
`(*&main)()', or compare it with another pointer, e.g. `if (fptr == &main)',
then the results are well-defined and do not depend on the implementation.

--
Fergus Henderson <fjh@cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3         |     -- the last words of T. S. Garp.
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: mangino@saturn.planet.net (Reed Mangino)
Date: 1997/04/07
Raw View
I know what the ARM has to say about explicitly calling main, taking
the address of main, etc.  Was this restriction strictly prohibited in
C also? (if not prohibited, is the call still system dependent?)

I can understand the problems with this practice, but I just can't
find a _definite_ answer for C.

Thank you for your time!
Reed

--

    ^^-__-^^-__-^^-__-^^-__-^^-__-^^-__-^^-__-^^-__-^^^-__-^^
     Reed R. Mangino       |  ** Dialogic Corporation **
     manginor@dialogic.com | World leader in the design of
     mangino@planet.net    |  computer telephony systems
   -----------------------------------------------------------
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: Stephen.Clamage@eng.sun.com (Steve Clamage)
Date: 1997/04/09
Raw View
In article 73o@jupiter.planet.net, mangino@saturn.planet.net (Reed Mangino) writes:
>I know what the ARM has to say about explicitly calling main, taking
>the address of main, etc.  Was this restriction strictly prohibited in
>C also? (if not prohibited, is the call still system dependent?)
>
>I can understand the problems with this practice, but I just can't
>find a _definite_ answer for C.

Like the ARM, the draft standard for C++ exlicitly disallows calling
or taking the address of main. C has different rules.

K&R1 (from 1978) says on page 6 that main is just a function, and
mentions no special restrictions.

The ISO C standard in 5.1.2.2.1 "Program startup" says that main is a
function and describes its properties. It places no restrictions on
calling or taking the address of main.

Section 5.1.2.2.3 "Program termination" says, "A return from the
initial call to the main function is equivalent to calling the
exit function ... " That wording confirms that recursive calls
to main are allowed.

---
Steve Clamage, stephen.clamage@eng.sun.com
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Barry Margolin <barmar@bbnplanet.com>
Date: 1997/04/09
Raw View
In article <5ibobl$73o@jupiter.planet.net>,
Reed Mangino <mangino@saturn.planet.net> wrote:
>I know what the ARM has to say about explicitly calling main, taking
>the address of main, etc.  Was this restriction strictly prohibited in
>C also? (if not prohibited, is the call still system dependent?)

C has no restrictions on calling main().

>I can understand the problems with this practice, but I just can't
>find a _definite_ answer for C.

The C specification doesn't say anything about it because there's nothing
to say.  About the only thing special about main() is that it's the
function run automatically in a hosted implementation.  The standard
doesn't need to list all the features it has in common with all other
functions you write -- they should be assumed.
--
Barry Margolin
BBN Corporation, Cambridge, MA
barmar@bbnplanet.com
(BBN customers, call (800) 632-7638 option 1 for support)
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: dacut@henry.ece.cmu.edu (David A. Cuthbert)
Date: 1997/04/09
Raw View
Steve Clamage <Stephen.Clamage@eng.sun.com> wrote:
>Like the ARM, the draft standard for C++ exlicitly disallows calling
>or taking the address of main. C has different rules.

Is there a reason why taking the address of main() is disallowed as
opposed to implementation defined?

As I mentioned on comp.lang.c++.moderated, this would have been useful
for a stack walkback library that I was toying with.  I came up with a
few workarounds, none of which were completely functional.

I do think that a diagnostic should be issued if a program takes the
address of main(); but the meaning of the value returned (if the
compiler is even willing to compile the code) should be implementation
defined (e.g., the first line of user code, the startup code, NULL,
etc.).

This seems to be one of the few areas in which the language rules
effectively prohibit the programmer from a dangerous action (instead
of guiding the programmer away from such an action).
--
David A. Cuthbert (henry.ece.cmu.edu!dacut)
Graduate Student, Electrical and Computer Engineering
Data Storage Systems Center, Carnegie Mellon University
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]