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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!ftpbox!motsrd!news
  3. From: shang@corp.mot.com (David (Lujun) Shang)
  4. Subject: Re: zero-length datatype
  5. Message-ID: <1992Sep10.181836.9929@cadsun.corp.mot.com>
  6. Sender: news@cadsun.corp.mot.com
  7. Reply-To: shang@corp.mot.com
  8. Organization: Motorola, Inc., Software Research and Development, Rolling Meadows, IL. 60008
  9. References: <1992Sep10.003726.27140@lucid.com>
  10. Distribution: comp
  11. Date: Thu, 10 Sep 92 18:18:36 GMT
  12. Lines: 88
  13.  
  14. In article <1992Sep10.003726.27140@lucid.com> jss@lucid.com (Jerry Schwarz)  
  15. writes:
  16. > In article <TMB.92Sep7171355@arolla.idiap.ch>, tmb@arolla.idiap.ch (Thomas M.  
  17. Breuel) writes:
  18. > |> C++ seems to be lacking in a data type that is guaranteed to be zero
  19. > |> length.
  20. > This understates the situation.  C++ guarantees that no object
  21. > will have size 0.  For example 
  22. >     struct Z { } ;
  23. > may not have size 0.
  24. >
  25.  
  26. Note that C g++ implements empty struct or class with size of zero. 
  27. g++ also allows an array with zero number of member with size of zero.
  28.  
  29. Zero size object is useful sometimes. For example:
  30.  
  31. struct Empty {};
  32.  
  33. class Chain
  34. {  TextChain * next;
  35.    TextChain * prev;
  36.    int node_size;
  37.    Empty chain_node;  // or  char chain_node[0];
  38.   public:
  39.    // operations on chain and chain node
  40. };
  41.  
  42. chain_node only provides the start address of the chain node. With
  43. size zero, it does not occupy any space. It would be awkward that 
  44. the field chain_node occupies an additional byte while it is useless
  45. and represents the wrong start address of the actual chain node. This
  46. technique is commonly used when templete is not available and you
  47. don't like to sprit memory into small pieces through many pointers.
  48. Even with template, you can still use this techique to avoid large
  49. static code expansion by presenting only a template interface which
  50. is actually impelemented at low level.
  51.  
  52. > One reason for this constraint is to ensure that no two objects
  53. > have the same address.  In particular the elements of an array
  54. > are always at different addresses.
  55. >
  56.  
  57. This is an implementation issue. The language itself has nothing to 
  58. do with a particular address protocol. An empty object needs no 
  59. storage to store its state, so we need not to take care of its 
  60. address, it can be anything by logic, i.e. to read and write through 
  61. an empty object address (pointer or reference) is logically equivalent 
  62. to null operation, and to compare two empty object addresses is 
  63. meaningless.
  64.  
  65. The implementation can assume the address of an empty object is where 
  66. it is defined so that its address may be used in some particular way 
  67. as I mentioned in the above example. But again, it is an implementation 
  68. issue.
  69.  
  70. >     Z a[10] ;
  71. > Without that constrain a[0],...,a[10] would all compare equal.
  72. > (I know a[10] isn't a real member, but ANSI C guarantees that
  73. > it is legal to take its address).
  74.  
  75. I don't see any reason why we should compare two empty objects. 
  76. An empty object has no state. It is meaningless to compare objects 
  77. without states. The result of the comparison is indeterminate. Of 
  78. cause the implementation can assume that the result of comparison 
  79. is always equal.
  80.  
  81. > Another is that  new Z will never return the same adddress.
  82. > Of course you could add this as an additional constraint on
  83. > operator new if you wanted to.
  84.  
  85. It doesn't matter what the address the new operator returns for an 
  86. empty object. As I said already, the address for an empty object is 
  87. meaningless.
  88.  
  89. > The constraint predates templates. Maybe it should be reconsidered.
  90.  
  91. Yes. I don't see zero-size objects would violate anything.
  92.  
  93. David Shang
  94.  
  95.