home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / c / 11654 < prev    next >
Encoding:
Text File  |  1992-07-27  |  4.5 KB  |  101 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!usc!cs.utexas.edu!torn!utzoo!telly!druid!darcy
  3. From: darcy@druid.uucp (D'Arcy J.M. Cain)
  4. Subject: Re: HIgher problems at hand. HELP!
  5. Message-ID: <1992Jul27.203435.15524@druid.uucp>
  6. Date: Mon, 27 Jul 1992 20:34:35 GMT
  7. References: <Brv6oD.Dq5@usenet.ucs.indiana.edu> <1992Jul25.142216.5636@druid.uucp> <9220806.7841@mulga.cs.mu.OZ.AU>
  8. Organization: D'Arcy Cain Consulting
  9. Lines: 90
  10.  
  11. fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON) writes:
  12. >darcy@druid.uucp (D'Arcy J.M. Cain) writes:
  13. >> * modified for ANSI by D'Arcy J.M. Cain
  14. >>/* The following define required for full checking.  The problem is that */
  15. >>/* the function has to ultimately assign a const char * to the return */
  16. >>/* value which has to be char *.  The answer is to drop the use of */
  17. >>/* const in this module and trust the code to do the right thing */
  18. >>#define        const
  19. >Yuck!
  20. >Do *not* #define keywords unless you have absolutely no alternative.
  21.  
  22. I didn't think that I did.  Note that I am not suggesting this kind of
  23. thing in ordinary programs.  Since strstr is a standard function this
  24. can be viewed as part of the implementation and of course you should
  25. take care that this works in your environment.  I think, however, that
  26. this method is the best solution and will work with most if not all
  27. current compilers.
  28.  
  29. >>#include    <string.h>
  30. >Now what happens if string.h happens to contain
  31. >    const CHAR_BIT = 8;
  32. >or some such?
  33.  
  34. Then you get rid of your compiler and get a standard one.  Exactly what
  35. do you think the above construct is doing?
  36.  
  37. >>char    *strstr(const char *s, const char *wanted)
  38. >This is a *different* prototype to the externally visible one, since
  39. >this one doesn't include "const". An implementation is well within it's
  40. >rights to give you a warning at link time, and probably even an error.
  41.  
  42. Well the prototype isn't visible to the linker however I think I see what
  43. you are getting at.  Perhaps it is theoretically possible that the calling
  44. sequence will be different for commands that take const pointers than ones
  45. that take non-const pointers but I would be really surprised.  Remember
  46. that functions that take consts must also accept non-consts as well.  The
  47. function has no way of knowing what kind it is, only if it is allowed to
  48. modify it **through the given pointer**.
  49.  
  50. >The solution to the const problem is just to put a cast in the final return
  51. >statement:
  52. >    return (const char *) s;
  53.  
  54. I suppose you mean "return (char *) s" since what you are returning is
  55. actually a const but the function needs to return a non-const.  Here is
  56. the real problem that I am attempting to overcome.  This, to my mind, is
  57. one of the two major flaws (*) in C.  There is no way to describe this
  58. type of function, one that may take one of a number of types and returns
  59. the type of the actual argument.  other examples of this type of function
  60. include memchr, memcmp, strchr, strpbrk and strrchr.  They all have to
  61. return a pointer to char but all they have is a pointer to const.  If
  62. you use Gnu and turn on all the warnings it will, properly IMO, warn
  63. you that you are doing something suspicious no matter how you code
  64. these functions because you *are* doing something suspicious.  In
  65. fact the standard prototypes for these functions actually lie since
  66. they claim that the functions return a non-const when in fact they don't
  67. always do so.  What C needs is a new qualifier which says in effect
  68. that a function returns the type qualified the same as its first
  69. argument.  I call it type "magic" and would work something like this.
  70.  
  71.     magic char *strstr(const char *, const char *);
  72.  
  73. and means that strstr takes a const but the return is so qualified only
  74. if the actual argument in a particular case is.  It's clumsy and I don't
  75. really suggest that it be considered for the standard but it illustrates
  76. the problem.  As it stands if you have something like:
  77.  
  78.     char *p;
  79.     const char *cp;
  80.     ...
  81.     p = strstr(cp, "SOME STRING");
  82.  
  83. you can modify the string pointed to by cp and there is no way that the
  84. compiler can catch it.
  85.  
  86. Of course the other option is to just ignore the warnings but I don't
  87. like to do that.
  88.  
  89.  
  90. (*) The other flaw IMO is that there is no way to describe a function
  91. that doesn't return.  Gnu allows functions to be qualified as volatile
  92. to implement this but of course that isn't portable.  Perhaps it will
  93. be considered existing practice by the time the standard is next up
  94. for review.
  95.  
  96. -- 
  97. D'Arcy J.M. Cain (darcy@druid.com)  |
  98. D'Arcy Cain Consulting              |   There's no government
  99. Toronto, Ontario, Canada            |   like no government!
  100. +1 416 424 2871          DoD#0082   |
  101.