Tricky optimization tricks

From: Mark Andrew Crichton (crichton@ecn.purdue.edu)
Date: 04/07/96


>       sprintf(buf, "%s%c ", buf, LOWER(*dirs[door]));
>       }
>     sprintf(buf2, "%s[ Exits: %s]%s\r\n", CCCYN(ch, C_NRM),
> +->	  *buf ? buf : "None! ", CCNRM(ch, C_NRM));
> |   send_to_char(buf2, ch);

Simple piece of code here.  It's a trigraph (?).

Basically if *buf is NULL (I.e. a null pointer), then use the string "None! ",
else, use the string in buf.  (Remember, FALSE == 0, TRUE != 0) 

This avoids an ugly if/else construct here, but it makes it more confusing to
the C newbie (even the C moderate and expert...there is a reason gcc has the
-Wtrigraphs option :) )

Alas, modern compilers SHOULD be able to optimize the if/else loop that would 
be there, but the additional sprintfs that (I think) would be necessary might
throw the optimizer off...anyone care to comment/compile with gcc -S -O2 to
check :)

Take Care,
Mark Crichton



This archive was generated by hypermail 2b30 : 12/18/00 PST