Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!usc!hacgate!ashtate!psivax!csun!mx!cbcscmrs From: mx!cbcscmrs@csun.edu Newsgroups: gnu.g++.bug Subject: Re: size of empty classes Message-ID: <2271@csun.edu> Date: 18 Aug 89 02:03:29 GMT References: <8908141613.AA23693@dsys.ncsl.nist.gov> Sender: news@csun.edu Reply-To: cbcscmrs@ma.csun.edu (Mike Stump) Distribution: gnu Organization: CSU, Northridge School of Engineering & Computer Science Lines: 45 In article <8908141613.AA23693@dsys.ncsl.nist.gov> rbj@DSYS.NCSL.NIST.GOV (Root Boy Jim) writes: >? From: chatty%FRLRI61.BITNET@cunyvm.cuny.edu > >? I just discovered that an empty class has a non-null size. Is that >? intentional? If it is, what are those 2 bytes for? Until now, I >? thought having an empty base class was for free. I am disappointed. > >? I use g++ 1.35.1- on a Sun3 running OS 3.5. > >Just a bit of speculation here. I hope you don't think all that >whiz-bang object-oriented stuff comes for free. Most likely those two >bytes are a pointer into some table telling you what class the thing >really is. Remember that when deriving one class from another, some >means of dynamically determining to which class an object belongs >must be provided. The higher level the language, the farther you >get from what the thing really is. Live with it. Well, consider it padding... (On a vax, with 1.35.0 anyway...) I have a simple class like: class oXdisplay { Display *dpy; dpy() { ... } ~dpy() { ... } }; And it does the Right Thing. It consumes a whole four bytes of memory per object. The Display* alone takes four bytes... But I DID find it interesting that an empty class on my machine did take a byte; just one. A class with just a char in it also took one byte. And a class with an int in it took 4 bytes. Note: due to memory alignments, all classes eat four bytes of memory apiece. The code, just if you want to see it is around line 729 of stor-layout.c in 1.35.0 and looks like: /* C++: do not let empty structures exist. */ if (const_size == 0 && var_size == 0) const_size = size_unit; So g++ does not need or use it, the question to the writers (Michael???) why force it up to one?