home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!taumet!steve
- From: steve@taumet.com (Steve Clamage)
- Subject: Re: A question about the sizeof operator
- Message-ID: <1992Jul31.170341.10204@taumet.com>
- Organization: TauMetric Corporation
- References: <drenze.712541103@icaen.uiowa.edu>
- Date: Fri, 31 Jul 1992 17:03:41 GMT
- Lines: 48
-
- drenze@icaen.uiowa.edu (Douglass J. Renze) writes:
-
- >I was playing around with the sizeof operator today, and I found out some-
- >thing curious.
-
- >class s1 {
- > struct name {
- > char last[10], first[10], mi;
- > };
- > s1* next;
- >};
-
- [ sizeof(s1) = 4 ]
-
-
- >Yet this next program...
-
- >class s1 {
- > char last[10], first[10], mi;
- > s1* next;
- >};
-
- [ sizeof s1 = 26 ]
-
-
- The first example defines a type called "name" inside s1. There is no
- instance of a "name" anywhere, so it doesn't take up any space.
-
- For example, if you added
- typedef int foo;
- typedef double bar;
- you wouldn't expect the size of the struct to change.
-
- Change the first example by adding a member of type 'name' to 's1':
-
- class s1 {
- struct name {
- char last[10], first[10], mi;
- } some_name; // now s1 has a member of type "name"
- s1* next;
- };
-
- Now the two versions should be (approximately) the same size. (There
- might be slightly different padding.)
- --
-
- Steve Clamage, TauMetric Corp, steve@taumet.com
- Vice Chair, ANSI C++ Committee, X3J16
-