Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!taumet!steve From: steve@taumet.com (Stephen Clamage) Newsgroups: comp.lang.c Subject: Re: Expressions in initializers Message-ID: <621@taumet.com> Date: 8 Mar 91 15:33:07 GMT References: <17270@crdgw1.crd.ge.com> <760@ajpo.sei.cmu.edu> <1991Mar4.144939.8311@ux1.cso.uiuc.edu> <1991Mar5.042141.21825@grebyn.com> Organization: Taumetric Corporation, San Diego Lines: 59 suhonen@kunto.jyu.fi (Timo Suhonen) writes: >rjc@uk.ac.ed.cstr (Richard Caley) writes: > ct> You are assuming that the compiler knows what the function sqrt(double) > ct> does. I think I've heard of a few that do. Most don't. > Is it possible to know before the program is linked (run if you have > run time linking!). What stops me from defining my own sqrt? >Doesn't ANSI standard let the compiler know the standard functions??? >If so, then sqrt(double) can be evaluated at compile time. And I >think that is just what every compiler SHOUD do! In Standard (ANSI/IOS) C, 'sqrt' is reserved for use as an external identifier, and reserved for use as a file-level identifier if is included. So consider the following C file ( NOT included): static double sqrt(double d) { return d / 2.0; } double x = sqrt(2.0); ... and more stuff ... If this were allowed and the compiler were to initialize x to 1.414... it would be wrong to do so. The correct initial value for x would be 1.0. If is included, the above is illegal, and the only interpretation for sqrt() is the one in . So the compiler could evaluate library functions with constant arguments when the appropriate headers were included. This starts getting a bit complicated, such as double x = sqrt((double)(srand(strlen("Hello")),rand())); which could be evaluated at compile time if , , and were all included. No, wait, srand() should affect the runtime environment, so we shouldn't evaluate this one at compile time. Golly, it IS getting complicated. Now consider cross-compilers. We would have to execute library functions for the target system on the host, using target system arithmetic, which may have data type sizes and formats different from the host. In the end, what have we saved by all this complication? It seems unlikely that initializing static variables is a large part of the execution time, or that significant extra code space would be taken up. If you have a loop like while(...) { ... x += sin(2.5); ... } you can save execution time by rewriting it as double sin25 = sin(2.5); while(...) { ... x += sin25; ... } (This may give less accurate results on systems, but there are no guarantees either way.) You do lose notational convenience by not being able to say double sqrt2 = sqrt(2.0); at the file level. C++ does allow this notation, but it evaluates the function calls at run time, conceptually before main() begins execution. Thus, with my first example, the static version of sqrt() get called, and with a less perverse program, the library sqrt() gets called. -- Steve Clamage, TauMetric Corp, steve@taumet.com