From owner-freebsd-hackers Tue Mar 5 7:13:49 2002 Delivered-To: freebsd-hackers@freebsd.org Received: from mailhub.fokus.gmd.de (mailhub.fokus.gmd.de [193.174.154.14]) by hub.freebsd.org (Postfix) with ESMTP id 9BD9737B402 for ; Tue, 5 Mar 2002 07:13:38 -0800 (PST) Received: from beagle (beagle [193.175.132.100]) by mailhub.fokus.gmd.de (8.11.6/8.11.6) with ESMTP id g25FDQR19875; Tue, 5 Mar 2002 16:13:27 +0100 (MET) Date: Tue, 5 Mar 2002 16:13:26 +0100 (CET) From: Harti Brandt To: "Eugene L. Vorokov" Cc: freebsd-hackers@FreeBSD.ORG Subject: Re: C vs C++ In-Reply-To: <200203051407.g25E7Cd67446@bugz.infotecs.ru> Message-ID: <20020305155850.R83273-100000@beagle.fokus.gmd.de> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG On Tue, 5 Mar 2002, Eugene L. Vorokov wrote: ELV>I have a small problem. I work for software development company and ELV>write daemons and console tools for Unix. My boss wants everything ELV>to be written in C++, because he thinks C++ is cool. I prefer C ELV>for such tasks, but I cannot really put good arguments of why and ELV>where C++ can be worse than C. I know many of you prefer C too. ELV>Can you please explain some disadvantages of C++ comparing to C ? ELV>Is it slower, does it produce less effective code, why is it like ELV>that, etc ... or please direct me to some articles where this can ELV>be explained. ELV> ELV>I apologize for the offtopic whenever it happens, but this issue ELV>really pisses me off now. Well there are (at least) two reasons, why I wouldn't use C++ for the type of program I suppose you are writing: 1. The machine model used by C more or less maps directly into the machines used by most computers. By looking at a statement you can more or less guess what the compiler will do. In C++ this is not the case for (at least) two features: exceptions and virtual methods. Both of these features have an overhead, that cannot be estimated in advance. You can of course not use these features. 2. C++ turns out to be harder to read except it is extremly good written. This stems from several features of the language: a) operator overloading. Seeing a '+' doesn't mean that something is added. It may even actuall be a subtraction. It may be an assignment (a once popular GUI library used '+' to add dialog elements to the window on the left side of the '+'). b) function overloading. In C it is simple to lookup a function when you find it called in a program. In C++ you have first to lookup the argument types. If these are objects, you have also to find out the entire inheritance hierarchy. Then you have a quite complicated ruleset to find out, what function is really called. c) hidden function calls all over the places. You never know, whether a statement does something simple or call a lot of functions. 'a = b' can invoke dozens of constructors and other functions you are not aware of. Also there were rumours that C++ is less efficient because the resulting object code tends to consist of many small functions scattered all over the address space calling each other. This may badly hit cache performance. Well, the main problem with this type of question is that the choice of language depends on the problem you try to solve. For number crunching Fortran may still be the language of choice, for GUI programming surely it is C++ (or objc or java, you name it), for inetd it is probably C. harti -- harti brandt, http://www.fokus.gmd.de/research/cc/cats/employees/hartmut.brandt/private brandt@fokus.fhg.de To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message