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

  1. Path: sparky!uunet!pipex!unipalm!uknet!mcsun!sunic!hagbard!loglule!jbn
  2. From: jbn@lulea.trab.se (Johan Bengtsson)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: zero-length datatype
  5. Message-ID: <4947@holden.lulea.trab.se>
  6. Date: 9 Sep 92 19:58:05 GMT
  7. References: <TMB.92Sep8141523@arolla.idiap.ch>
  8. Distribution: comp
  9. Organization: Telia Research AB, Aurorum 6, 951 75 Lulea, Sweden
  10. Lines: 45
  11. X-Newsreader: Tin 1.1 PL4
  12.  
  13. tmb@arolla.idiap.ch (Thomas M. Breuel) writes:
  14. : In article <MCGRANT.92Sep7142456@rascals.stanford.edu> mcgrant@rascals.stanford.edu (Michael C. Grant) writes:
  15. :    I don't understand why those 4 bytes are causing you such a stink. Perhaps
  16. :    you could enlighten us as to why a truly zero length data type is even
  17. :    necessary?
  18. : Under the current rules,
  19. :       struct nothing {};
  20. :       HashTable<int,nothing> table(1000000);
  21. : table takes up 8Mbytes (on most machines). If you could write
  22. :       HashTable<int,void> table(1000000);
  23. : table takes up 4Mbytes.
  24.  
  25. Note also that the "minimum 4 bytes" effect multiplies, if
  26. a class has more than one "void" data member:
  27.  
  28. // This is not legal C++ code, "void" means "zero-size type"
  29. class C {
  30.     long i;
  31.     void a,b,c,d,e;
  32. };
  33.  
  34. On common implementations, 1 million such objects take up
  35. 24 MB (since a-e take up 4 bytes each), whereas Thomas's
  36. proposed 'void' type means only 4 MB would be needed.
  37. Quite a difference!
  38.  
  39. Side note: As everybody knows, a similar size blowup occurs
  40. when small-size classes are given virtual methods, and those
  41. classes are used as data members in other classes. This is a
  42. good reason why not every class should have virtual methods.
  43.  
  44. Of course, a zero-size data type is not "necessary" (only wins
  45. you some memory), but it might be useful, IMHO.
  46.  
  47. -- 
  48. --------------------------------------------------------------------------
  49. | Johan Bengtsson, Telia Research AB, Aurorum 6, S-951 75 Lulea, Sweden  |
  50. | Johan.Bengtsson@lulea.trab.se; Voice:(+46)92075471; Fax:(+46)92075490  |
  51. --------------------------------------------------------------------------
  52.