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

  1. Path: sparky!uunet!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!moe.ksu.ksu.edu!hobbes.physics.uiowa.edu!news.uiowa.edu!icaen.uiowa.edu!drenze
  2. From: drenze@icaen.uiowa.edu (Douglass J. Renze)
  3. Newsgroups: comp.lang.c++
  4. Subject: A question about the sizeof operator
  5. Message-ID: <drenze.712541103@icaen.uiowa.edu>
  6. Date: 31 Jul 92 00:05:03 GMT
  7. Sender: news@news.uiowa.edu (News)
  8. Organization: University of Iowa, Iowa City, IA, USA
  9. Lines: 70
  10. Nntp-Posting-Host: t_ecn02.icaen.uiowa.edu
  11.  
  12. I was playing around with the sizeof operator today, and I found out some-
  13. thing curious.
  14.  
  15. The following program:
  16.  
  17. #include <iostream.h>
  18.  
  19. class s1 {
  20.         struct name {
  21.                 char last[10], first[10], mi;
  22.         };
  23.         s1* next;
  24. };
  25.  
  26. class s2 {
  27.         struct name {
  28.                 char last[10], first[10], mi;
  29.         };
  30. };
  31.  
  32. int main(void) {
  33.         cout << "sizeof s1 = " << sizeof(s1) << '\n';
  34.         cout << "sizeof s2 = " << sizeof(s2) << '\n';
  35.         return 1;
  36. }
  37.  
  38. produces the following output:
  39.  
  40. sizeof s1 = 4
  41. sizeof s2 = 1
  42.  
  43. Yet this next program...
  44.  
  45. #include <iostream.h>
  46.  
  47. class s1 {
  48.         char last[10], first[10], mi;
  49.         s1* next;
  50. };
  51.  
  52. class s2 {
  53.         char last[10], first[10], mi;
  54. };
  55.  
  56. int main(void) {
  57.         cout << "sizeof s1 = " << sizeof(s1) << '\n';
  58.         cout << "sizeof s2 = " << sizeof(s2) << '\n';
  59.         return 1;
  60. }
  61.  
  62. produces this next output...
  63.  
  64. sizeof s1 = 26
  65. sizeof s2 = 22
  66.  
  67. Naturally I didn't expect the "sizeofs" to be *identical* between the two
  68. programs (after all, one contains a struct within the class, and the other
  69. doesn't), however, I did expect them to be more nearly equal than that.  Can
  70. anybody explain this difference to me?  My curiosity is piqued.  Is it that
  71. the struct name isn't included within the class's memory allocation, or what?
  72.  
  73. Peace,
  74.  
  75. Doug
  76.  
  77. -- 
  78. | Douglas J Renze            | "Jerry [Brown] is the equivalent of a          |
  79. | drenze@icaen.uiowa.edu     |  drive-by shooting."                           |
  80. | drenze@chop.isca.uiowa.edu |                                                |
  81. | drenze@aol.com             |         * NPR's Morning Edition, 13 July 1992  |
  82.