home *** CD-ROM | disk | FTP | other *** search
- 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
- From: drenze@icaen.uiowa.edu (Douglass J. Renze)
- Newsgroups: comp.lang.c++
- Subject: A question about the sizeof operator
- Message-ID: <drenze.712541103@icaen.uiowa.edu>
- Date: 31 Jul 92 00:05:03 GMT
- Sender: news@news.uiowa.edu (News)
- Organization: University of Iowa, Iowa City, IA, USA
- Lines: 70
- Nntp-Posting-Host: t_ecn02.icaen.uiowa.edu
-
- I was playing around with the sizeof operator today, and I found out some-
- thing curious.
-
- The following program:
-
- #include <iostream.h>
-
- class s1 {
- struct name {
- char last[10], first[10], mi;
- };
- s1* next;
- };
-
- class s2 {
- struct name {
- char last[10], first[10], mi;
- };
- };
-
- int main(void) {
- cout << "sizeof s1 = " << sizeof(s1) << '\n';
- cout << "sizeof s2 = " << sizeof(s2) << '\n';
- return 1;
- }
-
- produces the following output:
-
- sizeof s1 = 4
- sizeof s2 = 1
-
- Yet this next program...
-
- #include <iostream.h>
-
- class s1 {
- char last[10], first[10], mi;
- s1* next;
- };
-
- class s2 {
- char last[10], first[10], mi;
- };
-
- int main(void) {
- cout << "sizeof s1 = " << sizeof(s1) << '\n';
- cout << "sizeof s2 = " << sizeof(s2) << '\n';
- return 1;
- }
-
- produces this next output...
-
- sizeof s1 = 26
- sizeof s2 = 22
-
- Naturally I didn't expect the "sizeofs" to be *identical* between the two
- programs (after all, one contains a struct within the class, and the other
- doesn't), however, I did expect them to be more nearly equal than that. Can
- anybody explain this difference to me? My curiosity is piqued. Is it that
- the struct name isn't included within the class's memory allocation, or what?
-
- Peace,
-
- Doug
-
- --
- | Douglas J Renze | "Jerry [Brown] is the equivalent of a |
- | drenze@icaen.uiowa.edu | drive-by shooting." |
- | drenze@chop.isca.uiowa.edu | |
- | drenze@aol.com | * NPR's Morning Edition, 13 July 1992 |
-