patch to suggest putc/fputs over printf("string") or printf("\n")

Ulrich Drepper drepper@cygnus.com
Mon Jan 11 10:53:00 GMT 1999


law@cygnus.com (Jeffrey A Law) writes:

> So, I still don't see why you object  to the optimization.

Because may introduce problems.  The compiler will never be able to
determine with 100% accuracy whether the transformation is correct.
If the uses is pointed to the case, instead, s/he can decide.

There's another point.  Take code like this:

	sprintf (buf, "a simple string");
	strcat (buf, "another string");

If this gets transformed into

	strcpy (buf, "a simple string"); 
	strcat (buf, "another string");

this still is suboptimal.  Using the functions from glibc which many
GNU packages already use it should be written

  memcpy (mempcpy (buf, "a simple string", sizeof ("a simple string") - 1),
          "another string", sizeof ("another string"))

or at least

  strcpy (stpcpy (buf, "a simple string"), "another string")


Letting the compiler do a suboptimal job will make developers even
lazier.  With a warning about the code the developer has the chance to
think about the code and maybe rewrite it in a better way.  In the
above example the sprintf call might be hidden in a macro call so it
is non-obvious at a first few.  The compiler could silently generate a
bit better code but only a warning will help to develop optimal code
since the rest cannot be done by the compiler alone.

-- 
---------------.      drepper at gnu.org  ,-.   1325 Chesapeake Terrace
Ulrich Drepper  \    ,-------------------'   \  Sunnyvale, CA 94089 USA
Cygnus Solutions `--' drepper at cygnus.com   `------------------------



More information about the Gcc-patches mailing list