Leo Willems) (12/02/90)
The ARM (the annotated C++ reference manual), section 3.4 states: "The function main() may not be called from within a program." I was looking for the same restriction in ANSI C, but could not find any statement in the ANSI C standard (2.1.2.2.1). Par. 2.1.2.2.3 (Program termination) states: "A return from the initial call to the main function...." The word "initial" suggests (to me) that main() may be called from within the program. Is C++ different from ANSI C on this point? If I didn't read the ANSI C standard careful enough, please mention the section where I can find the information. Thanks. Leo -- Leo Willems Internet: leo@atcmp.nl AT Computing UUCP: mcsun!hp4nl!kunivv1!atcmpe!leo P. O. Box 1428 6501 BK Nijmegen, The Netherlands Phone: +31-80-566880 Fax: +31-80-555887
steve@taumet.com (Stephen Clamage) (12/03/90)
leo@atcmp.nl (!Leo Willems) writes: |The ARM (the annotated C++ reference manual), section 3.4 states: | "The function main() may not be called from within a program." |I was looking for the same restriction in ANSI C, but could not find |any statement in the ANSI C standard (2.1.2.2.1). |Is C++ different from ANSI C on this point? Yes. In ANSI C, main() may be called from anywhere in the program. In C++, it may not be called, nor may its address be taken. Some reasons for this are given in the ARM. -- Steve Clamage, TauMetric Corp, steve@taumet.com
gwyn@smoke.brl.mil (Doug Gwyn) (12/03/90)
In article <814@atcmpe.atcmp.nl> leo@atcmp.nl (!Leo Willems) writes: >The word "initial" suggests (to me) that main() may be called from within the >program. I asked X3J11 for an interpretation on this point, and they confirmed that main() may be invoked "recursively" by a strictly conforming C program.
jimp@cognos.UUCP (Jim Patterson) (12/03/90)
In article <814@atcmpe.atcmp.nl> leo@atcmp.nl (!Leo Willems) writes: >The ARM (the annotated C++ reference manual), section 3.4 states: > > "The function main() may not be called from within a program." > >I was looking for the same restriction in ANSI C, but could not find >any statement in the ANSI C standard (2.1.2.2.1). >Par. 2.1.2.2.3 (Program termination) states: > > "A return from the initial call to the main function...." > >The word "initial" suggests (to me) that main() may be called from within the >program. At least some implementations treat the function 'main' in a special manner. Here's a snippet from a listing from VAX/VMS C V3.1-051 with the "/machine_code" flag on: 146 int main(void) { 0000 main: 0000 0000 .entry main,^m<> 5E 08 C2 0002 subl2 #8,sp 00000000* EF 16 0005 jsb C$MAIN_ARGS The subroutine C$MAIN_ARGS is only called by main, and the call is generated automatically by the compiler. I believe that it's purpose is to arrange for the argc and argv parameters to be provided to the main entry point. It's my guess that if you tried to call main() with different arguments with this compiler it wouldn't work (main would get the command line arguments no matter what you did, or something worse would happen). I won't hazard a guess as to whether this is considered legitimate behaviour according to the standard. -- Jim Patterson Cognos Incorporated UUCP:uunet!mitel!cunews!cognos!jimp P.O. BOX 9707 PHONE:(613)738-1440 3755 Riverside Drive NOT a Jays fan (not even a fan) Ottawa, Ont K1G 3Z4
gregk@cbnewsm.att.com (gregory.p.kochanski) (12/04/90)
Should functions be selected based on the type of their result? Generally, no. How about on the 'constness' of their result? Imagine we have a class with const and non-const members. I'm thinking particularly of an array class with reference counting: class array { pointer_to_class_with_data_and_reference_count ..... const int& operator[](int index) const; int& operator[](int index); }; Now in a situation like this, the two functions have drastically different costs to execute. The const version merely returns a reference to some spot in memory -- fast and cheap. On the other hand the non-const version has to copy the entire data array, on the assumption that an element is about to be modified. So far, so good. Now, suppose you're going to use this class, and you have a function that makes some modifications to the array: void x(array& q) { q[0] = q[1]; ... q[2] = q[3]; ... } Now, on half of these calls, we're just 'reading' from the array, but we're still paying the heavy overhead for the non-const functions. Admittedly, they could just as well be written as q[0] = ((const array&)q)[1], but in practice, that's a pain in the neck. So, one could propose that if both 'const' and 'non-const' versions of a function could be applied, and if the *result* could be const, then the 'const' version would be selected. This would make some code much more elegant. It's a real problem that I've run into several times. Is it worth the added complexity? Greg Kochanski gpk@physics.att.com
wgh@ubbpc.UUCP (William G. Hutchison) (12/05/90)
In article <535@taumet.com>, steve@taumet.com (Stephen Clamage) writes: > leo@atcmp.nl (!Leo Willems) writes: > > Yes. In ANSI C, main() may be called from anywhere in the program. > > Steve Clamage, TauMetric Corp, steve@taumet.com Yes: here is an example (K&R C) of echo.c (this will not win an obfuscated C contest, but it certainly is perverse!) #include <stdio.h> main(argc, argv) int argc; char *argv[]; { if (argc > 0) { fputs(*argv, stdout); fputs(" ", stdout); return main(--argc, ++argv); } puts(""); return 0; } -- Bill Hutchison, DP Consultant rutgers!cbmvax!burdvax!ubbpc!wgh (work) Unisys UNIX Portation Center uunet!eidolon!wgh (home) P.O. Box 500, M.S. B121 "At the moment I feel more like arguing than Blue Bell, PA 19424 being good" Raymond Smullyan _The Tao is Silent_
Reid Ellis <rae@gpu.utcs.toronto.edu> (12/09/90)
Gregory P. Kochanski <gregk@cbnewsm.att.com> writes: >void x(array& q) >{ > q[0] = q[1]; >... > q[2] = q[3]; >... >} > >Now, on half of these calls, we're just 'reading' from the array, >but we're still paying the heavy overhead for the non-const functions. >Admittedly, they could just as well be written as >q[0] = ((const array&)q)[1], You could also write the following: void x(array& q) { const array &qc = q; q[0] = qc[1]; ... q[2] = qc[3]; ... } But your 'array' class better not make any strange assumptions about 'clean' and 'dirty' arrays [i.e. the 'const' method should access the actual data, not some sort of cached value, unless you're remembering the array's current "dirtiness". Reid -- Reid Ellis 176 Brookbanks Drive, Toronto ON, M3A 2T5 Canada rae@gpu.utcs.toronto.edu || rae%alias@csri.toronto.edu CDA0610@applelink.apple.com || +1 416 446 1644