home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / c / 20060 < prev    next >
Encoding:
Internet Message Format  |  1993-01-22  |  3.2 KB

  1. Path: sparky!uunet!comp.vuw.ac.nz!acheron.amigans.gen.nz!alien
  2. From: alien@acheron.amigans.gen.nz (Ross Smith)
  3. Newsgroups: comp.lang.c
  4. Subject: Re:  can a pointer cast to a void* be compared to (void*)0 ?
  5. Message-ID: <alien.02pi@acheron.amigans.gen.nz>
  6. Date: 22 Jan 93 13:53:02 GMT+12
  7. References: <C0nzI2.CqM@cs.psu.edu> <1993Jan16.015545.26580@thunder.mcrcim.mcgill.edu>
  8. Distribution: world
  9. Organization: Muppet Labs
  10. Lines: 65
  11.  
  12. In article <1993Jan16.015545.26580@thunder.mcrcim.mcgill.edu> mouse@thunder.mcrcim.mcgill.edu (der Mouse) writes:
  13. >In article <C0nzI2.CqM@cs.psu.edu>, andrew@astro.psu.edu (Andrew Wilcox) writes:
  14. >>      char* c = 0;
  15. >>      void* p = (void*) c;
  16. >>      if (p == 0)
  17. >>        printf( "yes\n" );
  18. >
  19. >> Is this program guaranteed to print "yes"?
  20. >
  21. >Ah, this asks more.  You want to know if a null pointer of one type is
  22. >guaranteed to be a null-pointer-to-void when converted.  I don't think
  23. >this is guaranteed.  In the case of char *, the grandfather equivalence
  24. >of char * and void * may mean that it is, but I don't think so.  (The
  25. >equivalence means, I think, that you could memcpy c to p and thereby
  26. >ensure getting a null void *.  Of course, most systems will let it
  27. >work as you've written it, since it will usually require extra effort
  28. >to break it, but I don't think it's guaranteed.)
  29.  
  30. Sorry, you've got it backwards. (The FAQ explains this in all its gory
  31. detail :-)
  32.  
  33. A null pointer is required to remain a null pointer when cast to any other
  34. pointer type (except function pointers, which are a special case), and the
  35. integer zero and any null pointer are required to cast to to each other
  36. (except ditto). However, null pointers are *not* required to be actual
  37. bitwise zeros (and null pointers to different types are not required to be
  38. identical), so a memcpy() will *not* (necessarily) work. If a null pointer
  39. is not bitwise equivalent to the integer zero, the compiler must perform
  40. whatever magic is required to make them interchangeable in source code.
  41.  
  42. In other words, any ANSI compiler is required to treat the following code
  43. fragments as equivalent, *regardless* of what bit patterns are used for
  44. pointers:
  45.  
  46.    int *iptr = NULL;
  47.  
  48.    int *iptr = 0;
  49.  
  50.    int *iptr;
  51.    void *vptr = NULL;
  52.    iptr = (int *)vptr;
  53.  
  54. But in the last case, memcpy(&iptr, &vptr, sizeof(void *)) would not
  55. necessarily put a null pointer into iptr (in fact, sizeof(void *) and
  56. sizeof(int *) need not be the same).
  57.  
  58. (All this, of course, assumes that I've correctly understood the FAQ, which
  59. I'd be the first to admit is by no means a foregone conclusion :-)
  60.  
  61. Now a couple of questions of my own. I know (or think I know) that the
  62. standard does not require pointers to functions to be castable to/from any
  63. other pointer type.
  64.  
  65. (1) Are pointers to different types of function required to be castable to
  66. each other?
  67.  
  68. (2) If a compiler does allow casting between "ordinary" pointers and function
  69. pointers, does the rule about null pointers remaining null still apply?
  70.  
  71. --
  72. ...... Ross Smith (Wanganui, NZ) ...... alien@acheron.amigans.gen.nz ......
  73. "I blame you for the moonlit sky and the dream that died with the Eagle's flight
  74. I blame you for the moonlit nights when I wonder why are the seas still dry
  75. Don't blame me, sleeping satellite"                              (Tasmin Archer)
  76. --
  77.