home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / cplus / 13454 < prev    next >
Encoding:
Text File  |  1992-09-09  |  1.5 KB  |  55 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!stanford.edu!lucid.com!lucid.com!jss
  3. From: jss@lucid.com (Jerry Schwarz)
  4. Subject: Re: zero-length datatype
  5. Message-ID: <1992Sep10.003726.27140@lucid.com>
  6. Sender: usenet@lucid.com
  7. Reply-To: jss@lucid.com (Jerry Schwarz)
  8. Organization: Lucid, Inc.
  9. References:  <TMB.92Sep7171355@arolla.idiap.ch>
  10. Distribution: comp
  11. Date: Thu, 10 Sep 92 00:37:26 GMT
  12. Lines: 41
  13.  
  14. In article <TMB.92Sep7171355@arolla.idiap.ch>, tmb@arolla.idiap.ch (Thomas M. Breuel) writes:
  15. |> C++ seems to be lacking in a data type that is guaranteed to be zero
  16. |> length.
  17.  
  18.  
  19. This understates the situation.  C++ guarantees that no object
  20. will have size 0.  For example 
  21.  
  22.     struct Z { } ;
  23.  
  24. may not have size 0.
  25.  
  26. One reason for this constraint is to ensure that no two objects
  27. have the same address.  In particular the elements of an array
  28. are always at different addresses.
  29.  
  30.     Z a[10] ;
  31.  
  32. Without that constrain a[0],...,a[10] would all compare equal.
  33. (I know a[10] isn't a real member, but ANSI C guarantees that
  34. it is legal to take its address).
  35.  
  36. Another is that  new Z will never return the same adddress.
  37. Of course you could add this as an additional constraint on
  38. operator new if you wanted to.
  39.  
  40. The constraint predates templates. Maybe it should be reconsidered.
  41. However that would require some consideration of the effect 
  42. on existing code, and on common programming idioms.  For example
  43. something like
  44.  
  45.     Z a[10] ;
  46.     ... 
  47.     count = sizeof(a)/sizeof(Z)
  48.  
  49. suddenly becomes a divde by zero.
  50.  
  51.   -- Jerry Schwarz
  52.  
  53.  
  54.  
  55.