home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / c / 18810 < prev    next >
Encoding:
Internet Message Format  |  1992-12-22  |  3.9 KB

  1. Path: sparky!uunet!munnari.oz.au!goanna!ok
  2. From: ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: definition of strncpy stupid?
  5. Message-ID: <16298@goanna.cs.rmit.oz.au>
  6. Date: 23 Dec 92 04:21:39 GMT
  7. References: <1992Dec8.181543.15339@dnbf01.bram.cdx.mot.com> <1992Dec9.144743.1863@rti.rti.org>
  8. Organization: Comp Sci, RMIT, Melbourne, Australia
  9. Lines: 68
  10.  
  11. I realise this thread has been running for a while, but I think I have
  12. something new to say
  13.  
  14.  
  15. In article <1992Dec9.144743.1863@rti.rti.org>, jbs@rti.rti.org writes:
  16. > In article <1992Dec9.012534.15596@ptsfa.PacBell.COM> dmturne@PacBell.COM (Dave Turner) writes:
  17. > >>
  18. > >>I agree with the original poster in that there's a need for a function like
  19. > >>strncpy() but that null-terminates the target string. I find it a pain in
  20. > >
  21. > >The function already exists. It is called sprintf. Try:
  22. > >
  23. > >    sprintf(s1, "%.*s", n, s2);
  24. > sprintf() is slow. Part of the issue is execution speed.
  25.  
  26. I also proposed sprintf(), and got E-mail attacking my suggestion somewhat
  27. less than politely.  The blanket claim "sprintf() is slow" simply won't
  28. do.  Once upon a time the str*() functions were just plain old functions
  29. that were in the library because they were handy.  I still have my
  30. UNIX V7+ PDP-11 assembly code files in which I provided my own implementations
  31. of strcpy() and strlen().  (I needed to save a few dozen more bytes so that
  32. my program would fit, and I also needed the increase of speed I got by NOT
  33. using the library functions.)  Nobody has *EVER* promised that strncpy()
  34. would be fast.
  35.  
  36. On the other hand, X3J11 made explicit a few restrictions that already
  37. applied if you wanted to write portable code, which have the consequence
  38. that a C compiler is fully entitled to "know" about sprintf (and all the
  39. other library functions).  If you use 'sprintf' as an identifier with
  40. external linkage in one of your files, an ANSI C compiler is allowed to
  41. "know" about it in great detail.  (It may, for example, analyse the format
  42. string and give you a compile time warning if there is any mismatch between
  43. that string and the remaining arguments.)  In particular, for any of
  44. fprintf/printf/sprintf, if the format argument is a string literal, the
  45. compiler is entitled to analyse it and replace that call by something fast
  46. that has the same effect.  In particular, a C compiler is fully within its
  47. rights to replace
  48.     sprintf(s1, "%.*s", n, s2)
  49. by
  50.     __substr(s1, n, s2)
  51. where __substr is an implementation-specific substring function.
  52.  
  53. Your C compiler doesn't do this?  Well, tell the vendor that you wish it
  54. did.  Complain bitterly if it doesn't at least parse the string so that it
  55. can warn about argument msimatches (there are compilers that do it...).
  56. Get sprintf() included in a popular benchmark.
  57.  
  58. I've seen a "student" Fortran compiler on a slow machine outperform a
  59. "professional" optimizing Fortran compiler on a faster machine simply
  60. because the "student" compiler compiled FORMAT statements (and it gave
  61. better run-tiume diagnostics too, good one Dr Fenwick).  The same could
  62. be done for C, and it WOULD be done if enough people demanded it with
  63. their wallets in their hands.
  64.  
  65. Now, support sprintf() _is_ slow.  How will it be slow?  Well, it has to
  66. parse the format string (if the compiler hasn't done it already).  That's
  67. a start-up cost which is independent of n.  Then it has to move the
  68. bytes, maintaining a counter as well.  That's maybe 2 to 5 times slower
  69. than a special purpose substring function.  For small strings, BIG DEAL.
  70. For large strings, well, any program which is doing so much text manipulation
  71. that the difference between sprintf() and substr() _matters_ and isn't using
  72. some smarter representation (e.g. there are representations in which
  73. substr() is an O(1) operation) is so horribly badly written that it doesn't
  74. matter.  Even if there were a substr() function in the library, there are
  75. no tasks for which it would be dramatically better than sprinf() for which
  76. they wouldn't both of them be wrong.
  77.  
  78.