home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!swrinde!elroy.jpl.nasa.gov!ames!xn.ll.mit.edu!xn!haydens
- From: haydens@bullwinkle.juliet.ll.mit.edu (Hayden Schultz x3685 g42)
- Newsgroups: comp.lang.c++
- Subject: Re: zero-length datatype
- Message-ID: <HAYDENS.92Sep9215705@bullwinkle.juliet.ll.mit.edu>
- Date: 10 Sep 92 02:57:05 GMT
- References: <TMB.92Sep8141523@arolla.idiap.ch> <4947@holden.lulea.trab.se>
- Sender: usenet@xn.ll.mit.edu
- Reply-To: haydens@juliet.ll.mit.edu
- Distribution: comp
- Organization: M.I.T. Lincoln Lab - Group 42
- Lines: 110
- In-Reply-To: jbn@lulea.trab.se's message of 9 Sep 92 19:58:05 GMT
-
- In article <4947@holden.lulea.trab.se> jbn@lulea.trab.se (Johan Bengtsson) writes:
-
- tmb@arolla.idiap.ch (Thomas M. Breuel) writes:
- : In article <MCGRANT.92Sep7142456@rascals.stanford.edu> mcgrant@rascals.stanford.edu (Michael C. Grant) writes:
- :
- : I don't understand why those 4 bytes are causing you such a stink. Perhaps
- : you could enlighten us as to why a truly zero length data type is even
- : necessary?
- :
- : Under the current rules,
- :
- : struct nothing {};
- : HashTable<int,nothing> table(1000000);
- :
- : table takes up 8Mbytes (on most machines). If you could write
- :
- : HashTable<int,void> table(1000000);
- :
- : table takes up 4Mbytes.
-
- Note also that the "minimum 4 bytes" effect multiplies, if
- a class has more than one "void" data member:
-
- // This is not legal C++ code, "void" means "zero-size type"
- class C {
- long i;
- void a,b,c,d,e;
- };
-
- On common implementations, 1 million such objects take up
- 24 MB (since a-e take up 4 bytes each), whereas Thomas's
- proposed 'void' type means only 4 MB would be needed.
- Quite a difference!
-
- It's implied in this discussion that code better not either assign to
- or rely upon the value of a zero-size data member. Maybe I'm missing
- something, but why can't you use unions for this?
-
-
- #include <stream.h>
-
- typedef long Ignore;
-
- union C {
- long i;
- Ignore a,b,c,d,e;
- public:
- int Size() { return sizeof(*this); }
- };
-
- main()
- {
- C cobj;
-
- cout << "sizeof(C) is " << cobj.Size() << endl;
- }
-
- It prints:
-
- sizeof(C) is 4
-
-
- I don't know what is supposed to happen if you use or assign to a
- mythical zero-size datum, but it's awfully clear what will happen if
- you use or assign to a-e in the union above. You can protect them
- by making them private.
-
- You really shouldn't have to resort to zero-length data types anyway.
- I remember some code posted for a template HashTable class. It went
- something like this:
-
- template<class Key, class Value>
- class HashTable {
- public:
- Value lookup(const Key &) const;
- void add(const Key &);
- int in_table(Key &) const;
- };
-
- Please forgive any stupid template errors, my compilers don't have
- them yet. You should be able use abstraction to avoid this problem:
-
- template<class KeyNode>
- class HashTable {
- public:
- KeyNode lookup(const KeyNode &) const;
- void add(const KeyNode &);
- int in_table(KeyNode &);
- };
-
- This class assumes that KeyNode::Value() and KeyNode::Key() functions
- are used to access the key and value. When you want to only use the
- Value(), then you can write your KeyNode class:
-
- class NoValue {
- protected:
- long i;
- public:
- long Key() const { return i; }
- void Value() {
- cout << "NoValue::Value(), Gee, I guess I really did need a value!"
- << endl;
- abort();
- }
- };
-
- Hayden Schultz
- MIT Lincoln Lab
- haydens@juliet.ll.mit.edu
- haydens@atc.ll.mit.edu
-