TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: Neil Heller
from: Jasen Betts
date: 2004-04-13 08:01:54
subject: Squares

Hi Neil.

11-Apr-04 15:31:00, Neil Heller wrote to Bill Birrell



 NH> That bring up an interesting point.  It is such a common thing to
 NH> compare the loop counter to a result involving the loop counter in
 NH> some sort of function

 NH> (ie:  (for n=0; n that I would consider it only natural that any optimizing compiler
 NH> would do what you did, only automatically.  Is this the case?  Or
 NH> should I be concerned with optimizing manually - not trusting the
 NH> compiler manufacturer?

The compiler doesn't understand strlen.

the C copiler understands all the keywords processd the #defines etc
but all library functions are treated as black boxes.

So. it can't know that it always returns the same value if given the same
string, and even if it did it has no easy way to tell if the for loop will
modify the string.

assuming that target and the string are unmodifieed in the body of the
loop: the above for...  is better written

 for( n=0; target[n]; n++ ) {

which is another way of saying.

 (for n=0; target[n] != '\0' ; n++) {

and possibly even better re-written to use a char* instead

 for( s=target ; *s ; s++ ) {

 -=> Bye <=-

---
* Origin: If at first you don't succeed, the hell with it. (3:640/1042)
SEEN-BY: 633/267 270
@PATH: 640/1042 531 954 774/605 123/500 106/2000 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.