home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / cplus / 11862 < prev    next >
Encoding:
Text File  |  1992-07-31  |  1.4 KB  |  59 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!taumet!steve
  3. From: steve@taumet.com (Steve Clamage)
  4. Subject: Re: A question about the sizeof operator
  5. Message-ID: <1992Jul31.170341.10204@taumet.com>
  6. Organization: TauMetric Corporation
  7. References: <drenze.712541103@icaen.uiowa.edu>
  8. Date: Fri, 31 Jul 1992 17:03:41 GMT
  9. Lines: 48
  10.  
  11. drenze@icaen.uiowa.edu (Douglass J. Renze) writes:
  12.  
  13. >I was playing around with the sizeof operator today, and I found out some-
  14. >thing curious.
  15.  
  16. >class s1 {
  17. >        struct name {
  18. >                char last[10], first[10], mi;
  19. >        };
  20. >        s1* next;
  21. >};
  22.  
  23. [ sizeof(s1) = 4 ]
  24.  
  25.  
  26. >Yet this next program...
  27.  
  28. >class s1 {
  29. >        char last[10], first[10], mi;
  30. >        s1* next;
  31. >};
  32.  
  33. [ sizeof s1 = 26 ]
  34.  
  35.  
  36. The first example defines a type called "name" inside s1.  There is no
  37. instance of a "name" anywhere, so it doesn't take up any space.
  38.  
  39. For example, if you added
  40.     typedef int foo;
  41.     typedef double bar;
  42. you wouldn't expect the size of the struct to change.
  43.  
  44. Change the first example by adding a member of type 'name' to 's1':
  45.  
  46.     class s1 {
  47.         struct name {
  48.         char last[10], first[10], mi;
  49.         } some_name;    // now s1 has a member of type "name"
  50.         s1* next;
  51.     };
  52.  
  53. Now the two versions should be (approximately) the same size.  (There
  54. might be slightly different padding.)
  55. -- 
  56.  
  57. Steve Clamage, TauMetric Corp, steve@taumet.com
  58. Vice Chair, ANSI C++ Committee, X3J16
  59.