Xref: utzoo comp.object:2484 comp.lang.c++:11407 Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!think.com!linus!agate!darkstar!ucschu.ucsc.edu!ericg From: ericg@ucschu.ucsc.edu (Eric Goodman) Newsgroups: comp.object,comp.lang.c++ Subject: Virtual Functions (was: Inheritance and Information Hiding) Message-ID: <11797@darkstar.ucsc.edu> Date: 1 Feb 91 20:54:58 GMT Sender: usenet@darkstar.ucsc.edu Followup-To: comp.object Organization: UC Santa Cruz Lines: 39 References:<1991Jan23.224203.3206@runx.oz.au> <1991Jan24.214652.18515@Think.COM> <27A44871.5586@tct.uucp> <1991Jan29.221121.20642@odin.corp.sgi.com> In article <1991Jan29.221121.20642@odin.corp.sgi.com> linton@sgi.com (Mark Linton) writes: > As for virtual functions, if you can't live with cost of a virtual function call > you are unlikely to be happy with the cost of a direct function call. A virtual > function call typically adds a few memory references on top of what is likely > a considerably more expensive operation, involving register save/restore and > a branch. There is more than the indirect reference. There is the virtual function table which must be maintained for each object. If you are dealing with many (thousands) of small (2-4 int) objects, by using virtual functions you are adding at least 25%-50% extra size to your objects (assuming ints and the index into the virtual function table to be equal in size - it's probably worse than that). This leads into my question: when I declare a class with virtual functions how much memory am I losing per object? I was originally given to understand that for each virtual function a class maintains an index into a virtual function table, meaning extra memory size required is roughly proportional to the number of virtual functions. But thinking about it, that sounds too inefficient, so it should be something like a pointer to a virtual function table is created, meaning each object created uses extra memory for having virtual functions, but the amount of memory lost per object is constant whether one or several virtual functions are used in the class. Who wants to straighten me out? E-mail replies appreciated, or post and e-mail (I don't always get postings). Thanks Eric Goodman, UC Santa Cruz ericg@ucschu.ucsc.edu ericg@ucschu.bitnet Eric_Goodman.staff@macmail.ucsc.edu ...!ucbvax!ucscc!ucschu!ericg