home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / std / c / 3038 < prev    next >
Encoding:
Text File  |  1992-11-19  |  3.4 KB  |  75 lines

  1. Newsgroups: comp.std.c
  2. Path: sparky!uunet!wupost!zaphod.mps.ohio-state.edu!uunet.ca!wildcan!sq!msb
  3. From: msb@sq.sq.com (Mark Brader)
  4. Subject: Conversion to char* or void* (was: Must an object remain ...)
  5. Message-ID: <1992Nov19.202750.5142@sq.sq.com>
  6. Summary: An error of omission in the standard; revised posting
  7. Supersedes: <1992Nov19.062712.13431@sq.sq.com>
  8. Organization: SoftQuad Inc., Toronto, Canada
  9. References: <1992Nov18.104542.8499@thunder.mcrcim.mcgill.edu> <1992Nov19.061601.13164@sq.sq.com>
  10. Date: Thu, 19 Nov 92 20:27:50 GMT
  11. Lines: 62
  12.  
  13. This article supersedes version posted some hours ago, which omitted an
  14. important step of reasoning.  Apologies if you already saw, or posted a
  15. followup to, that article.  For the second time this week, my thanks go
  16. to Jutta Degener for email pointing out my error.
  17.  
  18.                 ---
  19.  
  20. In another subthread, I wrote:
  21.  
  22. > Consider *this* code:
  23. >     #include <comp/lang/c/FAQ-list.h> /* :-) */   /* for pstrcmp() */
  24. >     char *list[N_ENTRIES];
  25. >         /* read n (<= N_ENTRIES) strings into list */
  26. >     qsort (list, sizeof (char *), n, pstrcmp);
  27.  
  28. In addition to being an argument of intent in the situation I was writing
  29. about, this code also directs us to a similar argument in another matter,
  30. where I think the standard is deficient.
  31.  
  32. Suppose you had implemented a qsort() in standard C, and it was invoked as
  33. shown above.  Now consider the first argument.
  34.  
  35. Sections 3.2.2.3/6.2.3.3 and 3.3.4/6.3.4 allow a pointer to an arbitrary
  36. object (here, the decayed "list", of type char **) to be converted to
  37. either char * or void * and back without loss of information, so, in the
  38. presence of prototypes, the invocation is legal in that respect.  However,
  39. there is no specification in the standard of where the pointer resulting
  40. from conversion to char * must point!
  41.  
  42. I think the standard should specify that the pointer so obtained points
  43. to the FIRST byte of the object that the original (here, type char **)
  44. pointer was pointing to (here, the first byte of list[0]).  Without such
  45. a requirement, I can see no reasonable way for qsort() to be implemented
  46. in standard C and not break such simple code as the above -- for how on
  47. earth could it know where to find the elements of the array list, to do
  48. the necessary operations on them?
  49.  
  50. Speaking of qsort() is only suggestive; there's no requirement in the
  51. standard for qsort() to BE implementable in C.  But in fact the same
  52. problem arises with ANY function that has to access an object "for its
  53. bytes".  That is, any code that uses memcmp() or fread() or any of
  54. several other such library functions, to access an object which is not
  55. made up of elements of character type, faces a similar problem.
  56.  
  57. Since such code is commonplace today and there is often no other
  58. reasonable way to write it, I must conclude that the standard-writers
  59. intended that the conversion work as I said, but omitted to include
  60. a requirement specifying this.
  61.  
  62.  
  63. Note that this matter is entirely independent of the topic of the other
  64. subthread where the above code formed an example; in this one, it doesn't
  65. matter whether or not the objects in question are pointers.
  66. -- 
  67. Mark Brader            | "If you need features not found in any language,
  68. SoftQuad Inc., Toronto |  you can try your hand at creating your own.
  69. utzoo!sq!msb           | (Mind you, language design is incredibly difficult.
  70. msb@sq.com             |  It is easy to create an unholy mess.)" -- Chris Torek
  71.  
  72. This article is in the public domain.
  73.