Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!ut-emx!slcs.slb.com!asc.slb.com!hargrove From: hargrove@asc.slb.com (Jim Hargrove) Newsgroups: comp.object Subject: Re: C++ vs. C Message-ID: <1991Apr29.134820.21968@asc.slb.com> Date: 29 Apr 91 13:48:20 GMT References: <1991Apr24.125926.5146@asc.slb.com> <18905@crdgw1.crd.ge.com> <1991Apr25.213211.24114@asc.slb.com> Sender: news@asc.slb.com Organization: Schlumberger Austin Systems Center Lines: 36 In-Reply-To: jls@rutabaga.Rational.COM's message of 26 Apr 91 21:00:00 GMT Nntp-Posting-Host: nobelium >>>>> jls@rutabaga.Rational.COM (Jim Showalter) writes: >> ]AND, I think the technical superiority of the C++ implementation is >> ]significant. It isn't so important when there are only 4 cases. But >> ]what if there are 20 or so? Then the savings in memory and execution >> ]may be really important. >> Basically, the dynamic dispatching in C++ takes the same space and >> time as a non-inheritance version in C, and without decent tools the >> debugging is harder than in the C version. On the other hand, you >> get simpler looking code in C++, and you eliminate the dual-point-of- >> maintenance of the C version. Which is better for you is a very >> personal choice. >> -- Well, two posters have made this point, so I have to reply. The fact is that most compilers will generate better code for the virtual function dispatching than for the switch statement *in the specific case cited.* The reason is that the switch involved a number of *similar* function calls. Most compilers will not detect that the arguments being passed to the function are the same for all calls. As a consequence, the code to construct the calling sequence -- pushing parameters on the stack or whatever-- will be repeated for each case. I once converted a similar switch statement that contained 22 switches to use a table of procedures. The code size was reduced by about 4K. Maybe that wasn't a very smart compiler, or maybe it was a particularly poor architecture, but the point remains valid. I think that most compilers will generate better code using a virtual function table. -- -- jwh