<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Full of BS &#187; C++ Myths</title>
	<atom:link href="http://fullof.bs/category/programming/cc/c-myths/feed/" rel="self" type="application/rss+xml" />
	<link>http://fullof.bs</link>
	<description>He just never stops talking</description>
	<lastBuildDate>Thu, 15 Dec 2011 20:00:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Poor Articles And Falsehoods About C++</title>
		<link>http://fullof.bs/poor-articles-and-falsehoods-about-c-plus-plus/</link>
		<comments>http://fullof.bs/poor-articles-and-falsehoods-about-c-plus-plus/#comments</comments>
		<pubDate>Wed, 06 Sep 2006 18:53:09 +0000</pubDate>
		<dc:creator>John Haugeland</dc:creator>
				<category><![CDATA[C++ Myths]]></category>
		<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.sc.tri-bit.com/archives/169</guid>
		<description><![CDATA[It&#8217;s never been entirely clear to me how articles like this article on linuxdevices.com get cleared to be published.  If they&#8217;re onced over by someone with even half a clue about C++, they&#8217;d just get turned down.  It makes me worry about the quality of the magazine publishing them.  Nonetheless, someone used this article last [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s never been entirely clear to me how articles like <a title="Guh.  I mean, really." href="http://www.linuxdevices.com/eljonline/issue07/4870s1.html">this article on linuxdevices.com</a> get cleared to be published.  If they&#8217;re onced over by someone with even half a clue about C++, they&#8217;d just get turned down.  It makes me worry about the quality of the magazine publishing them.  Nonetheless, someone used this article last night to justify a stance borne largely in ignorance as regards C++, so it&#8217;s time to clear the air about the language.  Today, I start a new category to curb C++ naïveté and misapprehensions.  This is the first entry in &#8220;C++ Myths.&#8221;</p>
<p><span id="more-157"></span></p>
<p>The list of falsehoods in this article is both startling and borderline offensive to the adequate programmer.  I&#8217;ll just copy them as a list and respond to them individually.</p>
<ul>
<li><em><strong>C++&#8217;s virtues are expensive. Advanced OOP features, such as templates and the practice of using classes in the place of primitives, to name two examples, cause unacceptable code bloat.</strong></em></li>
<ul>
<li>Malarky.</li>
<li>Using classes in the place of machine types (there&#8217;s no such thing as a &#8220;primitive&#8221; in C or C++, you Java wank) has exactly zero overhead by design, except in those cases where one has RTTI turned on, which one should not for embedded.  Classes are structs and structs with a single member are, as in C, equivalent in storage to said member.  This is a requirement for C/C++ to match structs to hardware registers.</li>
<li>Using templates doesn&#8217;t cause massive bloat.  Templates are only instantiated for the types in which they are used.  For classes and structs this generates zero overhead without virtualizations, and one vtbl per finalized type with.  In either case, the comparison to a parallel C implementation requires the same functions to get filled out and used; this means that C ends up with at least as much binary, just hand-made.  Furthermore, C++ (since it knows what it&#8217;s up to) can frequently share large parts of, and occasionally the complete implementations of, said functions, meaning the C++ implementation &#8211; along with being shorter, cleaner more maintainable and always correctly linked code &#8211; is also frequently binary smaller.</li>
</ul>
<li><em><strong>A C++ compiler may generate many routines for one function (templates) or create routines where no function explicitly appeared (constructors, casts, etc.). There is generally a one-to-one relationship between a function in C code and the resulting machine-code routine. It&#8217;s easier to optimize what you can see than what you must infer. </strong></em></li>
<ul>
<li>Empty constructors are optimized out during link.  You might as well complain that C++ has constructors for integers.  They do not exist in the binary, take no space and are never called.  There is just as much a &#8220;one to one relationship&#8221; between C++ code and binary as there is with C, if you know what the code actually says.  Complaining that the language considers every type to have a constructor, when it gets optimized out and has zero impact, is just silly.  Wah wah, the language wants to be uniform and not have special cases that cost nothing.  The sky is falling and so is my soufflé.</li>
<li>The notion that there being a one-to-one relationship between your code and your binary output is either rare or significant is just absurd.</li>
<li>&#8220;<strong>It&#8217;s easier to optimize what you can see than what you must infer.</strong>&#8220; </li>
<ul>
<li>You don&#8217;t need to optimize things with no cost.</li>
<li>Premature optimization is the root of all <strike>evil</strike> stupidity.</li>
<li>To infer suggests that you had to do some detective work to ferret out that this is being done.  Sure, if you don&#8217;t know C++ you might have to infer some things about it.  This is equivalent to someone complaining about discovering the branch overhead of calling a function in C, because they didn&#8217;t know the language and didn&#8217;t know it&#8217;d happen.  You&#8217;re complaining that a language you don&#8217;t know does things you don&#8217;t know about.  Boo, hoo: read a book, Charlie.</li>
</ul>
</ul>
<li><em><strong>Virtual methods and polymorphism complicate runtime linking and require many relocations. This slows C++ application launch time considerably. C applications are both simple to link and amenable to lazy linking, so they load quickly. (For details, see Waldo Bastian&#8217;s paper &#8220;Making C++ Ready for the Desktop&#8221;, </strong></em><a href="http://www.suse.de/~bastian/Export/linking.txt" target="_blank"><em><strong>http://www.suse.de/~bastian/Export/linking.txt</strong></em></a><em><strong>.) </strong></em></li>
<ul>
<li>No.</li>
<li>Virtual methods and polymorphism <strong><em>allow</em></strong> runtime linking.  In C, you can&#8217;t have runtime linkage, unless you roll it yourself, and if you roll it yourself you&#8217;re just reinventing what C++ did.  Given that at runtime you don&#8217;t know as much about the nature of the code as the C++ compiler did, you are guaranteed to be unable to do as good a job as C++ does while remaining correct (read: safe) and portable.</li>
<li>Neither virtual methods nor polymorphism have anything to do with relocation. </li>
<ul>
<li>Relocatable code is code which is compiled to an address offset rather than an address.  This is useful for situations in which code is to be loaded at an unknown address, such as with dynamically linked libraries, static objects, and pretty much anything in a multithreaded environment.  If code is relocatable, then you can just load it at whatever address has space available, set the offset to that address, and wham, everything works.</li>
<li>Thing is, this isn&#8217;t how things work everywhere.  An embedded programmer should know this.  Non-relocatable code is compiled to a fixed address.  This is marginally faster, as it skips an addition during lookups and function calls, but that&#8217;s not tremendously important.  With relocation turned off, as is the case in most embedded environments, polymorphism and virtual methods still work just fine.  Ask anyone who writes games for a console video game system; because the programmer is in complete control of the machine, relocation is generally unnessecary, and so is generally disabled, but we still use polymorphism and virtualization extensively.</li>
</ul>
<li>Nothing <strong><em>ever</em></strong> requires multiple relocations.  It is contrary to the concept of location in a single dimensional memory space.  This is just retarded.</li>
<li>Relocation has zero impact on &#8220;C++ application launch time.&#8221;  Relocation is used in any application written for Windows, including not only C applications but also windows assembly applications.  Relocation has identical impacts on those applications as on C++ applications.  The impact that relocation has on the launch time of general applications is trivial: it&#8217;s the writing down of one single solitary offset.  It&#8217;s an allocation, a lookup and a write.  On most machines, it&#8217;s fewer than ten cycles.  You wouldn&#8217;t notice the impact on a computer from the 1950s.  Get over yourself.</li>
<li>The simplicity of linkage and the concept of lazy linkage have no effect on application load times whatsoever.  Linkage is a purely compile-time phenomenon.  None of the things done by the C++ runtime have anything to do with module linkage, nor with &#8220;runtime linkage.&#8221;  It is important to realize that the reason C++ doesn&#8217;t call it runtime linkage is that it isn&#8217;t equivalent in any way to what the compile-time linker does; that alternative name is an artifact from languages which are completely dynamically linked, such as Smalltalk, and shows a deep confusion about the actual nature of compilation.</li>
<li>C++ is just fine for lazy linkage, which is used extensively by most large C++ projects to reduce compile time.  C and C++ linkage are identical, other than C++ name decoration.  Anything the C linker can do, the C++ linker can do.  Their definitions in the standard are identical, other than name decoration rules and the ABI section that deals with passing the this pointer</li>
</ul>
<li><em><strong>Each class with virtual methods has an associated vtable array, which adds memory overhead. </strong></em></li>
<ul>
<li>So? </li>
<li>If you have a hundred classes with virtual methods, and each one has an average of three virtual methods, on a machine with 4-byte pointers that&#8217;s a walloping 1600 bytes.  Those vtbls are only made for virtual methods when there are multiple implementations of the method, and only when the class method or member is called through a base pointer; this overhead only exists if you&#8217;re actually using it. </li>
<li>This means that the only situation in which you could do it by hand in C in less space is on a processor like the ARM, where by switching to Thumb you could write smaller code than the compiler is inclined to generate.  Overhead doesn&#8217;t exist in a vacuum: it&#8217;s only there if you can do the same thing in C in less space.  In fact, given that in C you&#8217;re going to have to manually branch to a locally stored address, the C implementation is highly likely to end up larger.</li>
<li>Besides, if you&#8217;re in an environment where 1.6K makes a difference, you&#8217;re not going to have a hundred classes, each with three virtual members.  This is a &#8220;problem&#8221; which just doesn&#8217;t occur in the real world, even in the embedded world, at any scale.</li>
</ul>
<li><em><strong>C++&#8217;s tighter type-checking makes it difficult to write the space-conscious code reuse common to C applications (think: <tt>void *</tt>). </strong></em></li>
<ul>
<li>What the hell?  A void pointer works in C++ exactly the same way as it does in C, with the exception that you have to make all casts explicit in code.  Given that that has exactly zero binary impact, I can&#8217;t imagine why anyone ever believed this was true.</li>
<li>Even so, C++&#8217;s tighter typechecking actually means space-conscious code reuse is much <strong>easier</strong> and much more <strong>space efficient</strong> than C&#8217;s is, since you can use a single implementation and rely on stronger conversions through the OCR. </li>
<ul>
<li>If you don&#8217;t know that the OCR is the One Conversion Rule, you&#8217;re not qualified to discuss typal issues in C or C++.</li>
</ul>
</ul>
<li><em><strong>The small, simple code demanded of embedded projects provides maintainability. There is no reason to assume OOP will further simplify such systems. </strong></em></li>
<ul>
<li>Yes, C++ is bad because you don&#8217;t understand it well enough to see a benefit coming a mile away.  (cough)  This kind of hubritic nonsense even looks out of place on the internet.  Hang your head, shame-boy.</li>
<li>The specific purpose of objects is to provide local simplicity and maintainability through modularity and maintained interfaces.  This is explained in the first two pages of <a title="Stroustrup" href="http://sc.tri-bit.com/tcpppl">The C++ Programming Language</a>.  It&#8217;s obvious you haven&#8217;t read the book.  Funny how you still feel empowered to criticize something you plainly don&#8217;t understand&#8230;</li>
</ul>
<li><em><strong>GUIs may not have a simple solution in a rigorous OOP model. </strong></em></li>
<ul>
<li>Have you ever used an object-driven GUI system?  GUIs are the poster child for how objects can make life easier.  Essentially every GUI in existence is built on an inheritance model.  Granted, most GUIs provide a C interface because C++ linkage isn&#8217;t nearly as universal; that doesn&#8217;t mean it&#8217;s any less OO.</li>
<li>Whether GUIs have a solution in an OOP model isn&#8217;t germane.</li>
<ul>
<li>GUIs have no better solution in C than C++.</li>
<li>GUIs are rarely a concern in embedded development.  Do not confuse developing Linux applications for a small machine you hold in your hand with embedded development; embedded development means to the bare metal, without an operating system, typically in ROM.  Most embedded applications have no interface at all; they control things like your anti-lock brakes, the temperature coming out of your air conditioner and whether that key card just unlocked that door.</li>
<li>There are literally dozens of GUI libraries available to C and C++.  That the language explicitly chooses not to implement them is a tip of the hat to that real embedded developers generally cannot use the kind of library that most people would want in the PC arena.  That C++ chooses to skip GUIs is in fact a huge win for embedded, not a loss.  An embedded C developer should know that, as it has the exact same win.</li>
</ul>
<li>This is absurdism coupled with naïvété, plain and simple.</li>
</ul>
<li><em><strong>It&#8217;s easy to get carried away and start doing OOP for OOP&#8217;s sake. The One True Object Model may describe a problem perfectly, but it comes at the cost of excessive code. </strong></em></li>
<ul>
<li>Yes, it&#8217;s true that bad programmers can get stuck on a near-religious devotion to a language feature.  If you&#8217;re choosing languages so that there are not useful features, to prevent your programmers from doing stupid things, you need to start hiring better programmers. </li>
<li>You might just as well suggest that C is not an acceptable language because some programmers get wholly stuck on implementing a system as a huge ball of function pointers, and that all embedded should be assembly.</li>
</ul>
<li><em><strong>Carefully written C code can be much faster than C++ code, especially on embedded hardware. GTK+&#8217;s hand-crafted object system offers much better spatial locality than C++&#8217;s more numerous and distributed constructors. Our device has a tiny cache, so locality is an especially important performance consideration.</strong> </em></li>
<ul>
<li>There is no reason for you to suggest that C can be much faster than C++, and time and time again benchmarks prove that this is not just false, but backwards.  The C++ compiler knows more about its code than does the C compiler, and therefore can establish that some things (notably sub-const typal concerns and volatility issues are a big deal here) are safe when the C compiler wouldn&#8217;t be able to.  As a result, the C++ compiler can make more aggresive optimizations than can the C compiler.</li>
<li>GTK&#8217;s hand-crafted object system exists as a legacy system to support C and ASM applications, just like Windows&#8217;.  It doesn&#8217;t offer better spatial locality than C++ GUI systems would; it uses the same malloc that GCC&#8217;s new is implemented with.  In fact, a C++ GUI can provide better spatial locality than a C GUI can, because of things like overloading operator new, which allows the reuse of code segments that C would have seperated.  On embedded machines, where ITCM caches are frequently 8-32k, that space savings can be significant.</li>
<li>&#8220;<strong>C++&#8217;s more numerous and distributed constructors</strong>&#8220;</li>
<ul>
<li>&#8230; FUD much?  Unused constructors have 0 binary impact, and used constructors are guaranteed as small as or smaller than their C implementation counterparts.  The count thereof is unimportant, as they&#8217;re only going to be there if they&#8217;re doing things, and so in the C parallel implementation a minimum of the same count of things will be implemented.</li>
<li>Distributed constructors?  In the words of Seth McFarlane, &#8220;can I buy some pot from you?&#8221;  They&#8217;re just functions.  They&#8217;re kept in the .code segment just like everything else.</li>
</ul>
<li>&#8220;<strong>Our device has a tiny cache, so locality is an especially important performance consideration.</strong>&#8220;</li>
<ul>
<li>So co-implement operator new, which will save you space on things with construction behavior.  Things without construction behavior won&#8217;t be generating any constructors anyway.</li>
</ul>
</ul>
</ul>
<p>Anyway, that&#8217;s that for this particular list, but lord knows I&#8217;ve found enough other articles like this spreading misapprehension and outright falsehoods about C++.  Maybe, with time, I&#8217;ll clear up some others, too.</p>
]]></content:encoded>
			<wfw:commentRss>http://fullof.bs/poor-articles-and-falsehoods-about-c-plus-plus/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

