Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site harvard.ARPA Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!think!harvard!stew From: stew@harvard.ARPA (Stew Rubenstein) Newsgroups: net.lang.c Subject: Re: C programming hint Message-ID: <257@harvard.ARPA> Date: Wed, 17-Jul-85 02:48:28 EDT Article-I.D.: harvard.257 Posted: Wed Jul 17 02:48:28 1985 Date-Received: Thu, 18-Jul-85 07:18:55 EDT References: <899@teddy.UUCP> <729@asgb.UUCP> <730@asgb.UUCP> Reply-To: stew@harvard.UUCP (Stew Rubenstein) Organization: Aiken Computation Laboratory, Harvard Lines: 34 Keywords: strncpy optimization Summary: You're a dreamer In article <730@asgb.UUCP> seismo!hao!asgb!mah (Mark Hamilton) writes: >> > I found a way to initialize an array of characters without using a loop. >> > Here is the method I used: >> > strncpy(blanks + 1, blanks, SIZE - 1); /* initialize entire array */ >> >> Unfortunately, strncpy uses the "loop" that you are trying to avoid. It >> also makes a redundant (in this case) check for the null character each time >> through the loop that should be avoided. > >One would hope that the compiler (or optimizer) would realize what's >going on here, and do something a little more clever than looping. >Especially since this is a strncpy, so the check for the null character >isn't done. I must admit that I am new to Unix C, but on VMS (and I >beleive TOPS20, for whoever uses that) C, the compiler will turn this >into a MOVC instruction (or a BLT on the '20). These are both the accepted >method for filling a region. > >Mark Hamilton You are dreaming if you think that the compiler will turn a function call into a single inline instruction. This is an admirable dream, but a dream nonetheless. This gets compiled (by VMS VAX-11 C Version 2.0) into a subroutine call. There's no way to assure the compiler that this strncpy is going to be pulled from the standard run-time library. Suppose you wanted to replace the standard strncpy (or more likely some other standard library function, like malloc) with your own for some reason (debugging? better error handling?) Now won't you be annoyed if the compiler optimizes this function call away? Anyone know of any efforts to standardize an indication to the compiler that it is or is not OK to compile standard library functions in-line? Stew {seismo,ut-sally}!harvard!rubenstein