From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=0.2 required=5.0 tests=BAYES_00,INVALID_MSGID, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fc89c,97188312486d4578 X-Google-Attributes: gidfc89c,public X-Google-Thread: 109fba,baaf5f793d03d420 X-Google-Attributes: gid109fba,public X-Google-Thread: 1014db,6154de2e240de72a X-Google-Attributes: gid1014db,public X-Google-Thread: 103376,97188312486d4578 X-Google-Attributes: gid103376,public From: mdw@excessus.demon.co.uk (Mark Wooding) Subject: Re: What's the best language to start with? [was: Re: Should I learn C or Pascal?] Date: 1996/08/26 Message-ID: #1/1 X-Deja-AN: 176800582 x-nntp-posting-host: excessus.demon.co.uk references: <31FBC584.4188@ivic.qc.ca> <01bb83f5$923391e0$87ee6fce@timpent.airshields.com> <4uah1k$b2o@solutions.solon.com> <01bb853b$ca4c8e00$87ee6fce@timpent.airshields.com> <4udb2o$7io@solutions.solon.com> <01bb8569$9910dca0$87ee6fce@timpent.airshields.com> <4urqam$r9u@goanna.cs.rmit.edu.au> <01bb8b84$200baa80$87ee6fce@timpent.airshields.com> <4vbbf6$g0a@goanna.cs.rmit.edu.au> <01bb8f18$713e0e60$32ee6fce@timhome2> <4vroh3$17f@goanna.cs.rmit.edu.au> organization: Straylight Development Lab reply-to: mdw@excessus.demon.co.uk newsgroups: comp.lang.c,comp.lang.c++,comp.unix.programmer,comp.lang.ada Date: 1996-08-26T00:00:00+00:00 List-Id: Richard A. O'Keefe wrote: > - standard optimisations like common subexpression elimination, can be > applied, so that any number of calls to strlen(s) without any > intervening potential change to s are no more expensive than a > single call and then later: > strlen() is a pure function and its argument does not change, so the > strlen(s) computation can be hoisted out of the loop. Erk! No it isn't. A pure function is one whose value depends only on its arguments. sin() is pure. strlen() isn't. The argument to strlen() is a pointer to a string whose length we want. It's the address, not the string itself. Because the string can change between calls to strlen(), it might give different results given the same string address. So the compiler can't just use its general `pure' function mechanism for common-subexpression-optimising strlen() Now, can a compiler do clever things and optimise strlen() all by itself? You comment that it might spot assignments to the string. This is true, but not all such assignments are visible to the compiler. For automatic buffers, this /is/ true, but (in my experience) calls to strlen() and similar functions are comparitively rarerely used on locally allocated buffers. If the buffer is not local to the function, there's no guarantee that (in an extreme case) its address hasn't been made available to a signal handler which maliciously changes the string's length. Prove me wrong. -- [mdw] `When our backs are against the wall, we shall turn and fight.' -- John Major