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

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!swrinde!elroy.jpl.nasa.gov!ames!xn.ll.mit.edu!xn!haydens
  2. From: haydens@bullwinkle.juliet.ll.mit.edu (Hayden Schultz x3685 g42)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: zero-length datatype
  5. Message-ID: <HAYDENS.92Sep9215705@bullwinkle.juliet.ll.mit.edu>
  6. Date: 10 Sep 92 02:57:05 GMT
  7. References: <TMB.92Sep8141523@arolla.idiap.ch> <4947@holden.lulea.trab.se>
  8. Sender: usenet@xn.ll.mit.edu
  9. Reply-To: haydens@juliet.ll.mit.edu
  10. Distribution: comp
  11. Organization: M.I.T. Lincoln Lab - Group 42
  12. Lines: 110
  13. In-Reply-To: jbn@lulea.trab.se's message of 9 Sep 92 19:58:05 GMT
  14.  
  15. In article <4947@holden.lulea.trab.se> jbn@lulea.trab.se (Johan Bengtsson) writes:
  16.  
  17.    tmb@arolla.idiap.ch (Thomas M. Breuel) writes:
  18.    : In article <MCGRANT.92Sep7142456@rascals.stanford.edu> mcgrant@rascals.stanford.edu (Michael C. Grant) writes:
  19.    : 
  20.    :    I don't understand why those 4 bytes are causing you such a stink. Perhaps
  21.    :    you could enlighten us as to why a truly zero length data type is even
  22.    :    necessary?
  23.    : 
  24.    : Under the current rules,
  25.    : 
  26.    :       struct nothing {};
  27.    :       HashTable<int,nothing> table(1000000);
  28.    : 
  29.    : table takes up 8Mbytes (on most machines). If you could write
  30.    : 
  31.    :       HashTable<int,void> table(1000000);
  32.    : 
  33.    : table takes up 4Mbytes.
  34.  
  35.    Note also that the "minimum 4 bytes" effect multiplies, if
  36.    a class has more than one "void" data member:
  37.  
  38.    // This is not legal C++ code, "void" means "zero-size type"
  39.    class C {
  40.        long i;
  41.        void a,b,c,d,e;
  42.    };
  43.  
  44.    On common implementations, 1 million such objects take up
  45.    24 MB (since a-e take up 4 bytes each), whereas Thomas's
  46.    proposed 'void' type means only 4 MB would be needed.
  47.    Quite a difference!
  48.  
  49. It's implied in this discussion that code better not either assign to
  50. or rely upon the value of a zero-size data member. Maybe I'm missing
  51. something, but why can't you use unions for this?
  52.  
  53.  
  54. #include <stream.h>
  55.  
  56. typedef long    Ignore;
  57.  
  58. union C {
  59.     long    i;
  60.     Ignore    a,b,c,d,e;
  61.   public:
  62.     int        Size() { return sizeof(*this); }
  63. };
  64.  
  65. main()
  66. {
  67.     C    cobj;
  68.  
  69.     cout << "sizeof(C) is " << cobj.Size() << endl;
  70. }
  71.  
  72. It prints:
  73.  
  74. sizeof(C) is 4
  75.  
  76.  
  77. I don't know what is supposed to happen if you use or assign to a
  78. mythical zero-size datum, but it's awfully clear what will happen if
  79. you use or assign to a-e in the union above. You can protect them
  80. by making them private.
  81.  
  82. You really shouldn't have to resort to zero-length data types anyway.
  83. I remember some code posted for a template HashTable class. It went
  84. something like this:
  85.  
  86. template<class Key, class Value>
  87. class HashTable {
  88.   public:
  89.     Value    lookup(const Key &) const;
  90.     void    add(const Key &);
  91.     int        in_table(Key &) const;
  92. };
  93.  
  94. Please forgive any stupid template errors, my compilers don't have
  95. them yet. You should be able use abstraction to avoid this problem:
  96.  
  97. template<class KeyNode>
  98. class HashTable {
  99.   public:
  100.     KeyNode    lookup(const KeyNode &) const;
  101.     void    add(const KeyNode &);
  102.     int        in_table(KeyNode &);
  103. };
  104.  
  105. This class assumes that KeyNode::Value() and KeyNode::Key() functions
  106. are used to access the key and value.  When you want to only use the
  107. Value(), then you can write your KeyNode class:
  108.  
  109. class NoValue {
  110.   protected:
  111.     long    i;
  112.   public:
  113.     long    Key() const { return i; }
  114.     void    Value() {
  115.     cout << "NoValue::Value(), Gee, I guess I really did need a value!"
  116.         << endl;
  117.     abort();
  118.     }
  119. };
  120.  
  121.     Hayden Schultz
  122.     MIT Lincoln Lab
  123.     haydens@juliet.ll.mit.edu
  124.     haydens@atc.ll.mit.edu
  125.