home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!synaptx!synaptics.com!daveg
- From: daveg@synaptics.com (Dave Gillespie)
- Newsgroups: comp.lang.c++
- Subject: Re: Sizeof and Virtual functions
- Message-ID: <DAVEG.92Dec24195757@synaptx.synaptics.com>
- Date: 25 Dec 92 03:57:57 GMT
- References: <1992Dec24.061254.9763@nuscc.nus.sg> <1992Dec24.132121.1@vax1.bham.ac.uk>
- Sender: daveg@synaptx.Synaptics.Com
- Organization: Synaptics, Inc., San Jose, CA
- Lines: 50
- In-reply-to: mccauleyba@vax1.bham.ac.uk's message of 24 Dec 92 13:21:21 GMT
-
- In article <1992Dec24.132121.1@vax1.bham.ac.uk> mccauleyba@vax1.bham.ac.uk (Brian McCauley) writes:
-
- >> Isn't there a better way to implement virtual functions ?
- > No. The only other way would be to include the type information in the pointer
- > itself so all pointers to objects of classes with virtual functions would
- > become bigger. This would also mean that if a pointer were converted to void*
- > and back it would loose it's type information. (I'm not sure if a compiler
- > with this property would be conforming but it would certainly be unpopular).
-
- Oh, surely you're not saying there is *no possible* other way to
- implement virtual functions than this.
-
- Consider: Suppose you divided your memory space into pages, say of
- size 1K. Arrange so that a given page only contains objects of a
- single given class (with virtual functions). Make a table that maps
- from page numbers onto virtual function tables. If objects are small
- enough so that many can fit on a single page, the virtual overhead is
- essentially removed. And in particular, the overhead is moved out of
- the object itself, so we've removed the effect on the value of `sizeof'.
-
- (Now, this ignores the issue of stack allocation of objects, or
- inclusion of objects as members of other objects. It would take
- a lot of work to iron out all the details, but my point is, it's
- *possible* to implement virtual functions in a totally different
- way if that's what you really need.)
-
- > Another solution would be to adjust the action of the `sizeof' and `(void*)'
- > operators to refer to the object excluding its type indicator but
- > this would break any code that used `malloc'.
-
- It would break all kinds of things, in fact.
-
- >> Isn't the current implementation a clever hack at best ?
- > Possibly but the only alternative would be a clumsy hack.
-
- Or something better that nobody's thought of, yet.
-
- > Almost any use of void* and sizeof is implementation dependant, they are
- > really part of C and C++ programmers should avoid them whenever possible.
-
- An excellent point. The `new' operator was a fine innovation in C++,
- even aside from its necessity when constructors are involved. It
- removes the one place in C where `sizeof' was needed in everyday
- programs.
-
- -- Dave
- --
- Dave Gillespie
- daveg@synaptics.com, uunet!synaptx!daveg
- or: daveg@csvax.cs.caltech.edu
-