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

  1. Path: sparky!uunet!autodesk!larsn
  2. From: larsn@Autodesk.COM (Lars Nyman)
  3. Newsgroups: comp.std.c
  4. Subject: pointer comparison
  5. Message-ID: <18221@autodesk.COM>
  6. Date: 23 Dec 92 00:14:52 GMT
  7. Sender: news@Autodesk.COM
  8. Lines: 37
  9.  
  10.  
  11. There is a thread in comp.std.c++ about comparisons of pointers
  12. to objects that caused me to think about the situation in ANSI C.
  13. I don't have the ANSI C standard and thus resort to ask here.
  14.  
  15. I *think* there is language in the standard that says something to the
  16. effect that comparisons of pointers to two objects that are NOT
  17. members of the same aggregate or union object is undefined (with
  18. one exception - one or both pointers pointing past the last object
  19. of an array).
  20.  
  21. So, if I have two different objects of same type, and they are not
  22. in the same array or aggregate, the result of comparing pointers to
  23. them is undefined !!???
  24.  
  25. Which would mean that in the following trivial code:
  26.  
  27.     typedef struct { int i; } foo;
  28.  
  29.     foo f1, f2;
  30.     foo* pF1 = &f1;
  31.     foo* pF2 = &f2;
  32.  
  33.     assert( pF1 != pF2 );
  34.  
  35. the value of 'pF1 != pF2' is undefined and the assertion may/can fail !!!
  36.  
  37. Can somebody, please, correct me and tell me what I am overlooking.
  38. What in the standard guarantees that pointers to two different objects
  39. are guaranteed to compare unequal ?
  40.  
  41. Lars Nyman
  42.  
  43. (*If* my reasoning is correct... wouldn't this also allow malloc(0) to
  44.  return whatever it wants, including repeatedly the same value (except 
  45.  maybe for NULL), since the result of a comparison between any two pointers 
  46.  obtained by malloc() would be undefined).
  47.