<?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>Honest to a Segfault &#187; Embedded</title>
	<atom:link href="http://blog.cdleary.com/tag/embedded/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.cdleary.com</link>
	<description>__author__ = &#039;Chris Leary&#039;</description>
	<lastBuildDate>Sun, 05 Sep 2010 20:41:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Thoughts on C++ in small memory footprint embedded development</title>
		<link>http://blog.cdleary.com/2008/07/thoughts-on-cpp-in-small-memory-footprint-embedded-development/</link>
		<comments>http://blog.cdleary.com/2008/07/thoughts-on-cpp-in-small-memory-footprint-embedded-development/#comments</comments>
		<pubDate>Tue, 29 Jul 2008 09:41:39 +0000</pubDate>
		<dc:creator>cdleary</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[AVR]]></category>
		<category><![CDATA[Cpp]]></category>
		<category><![CDATA[Embedded]]></category>

		<guid isPermaLink="false">http://blog.cdleary.com/?p=71</guid>
		<description><![CDATA[Background During my senior year I took on an ECE491 Independent Project course to follow up on a ECE476 Microcontrollers project. On the completion of ECE491 we had created a Low Speed USB 2.0 stack library for the Atmel Mega32 Microcontroller using ~$6 worth of hardware and ~6000 standard lines of C. Everybody in ECE476 [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Background</strong></p>
<p>During my senior year I took on an <a href="http://instruct1.cit.cornell.edu/courses/eceprojectsland/STUDENTPROJ/2007to2008/blh36_cdl28_dct23/index.html">ECE491 Independent Project</a> course to follow up on a <a href="http://instruct1.cit.cornell.edu/courses/ee476/FinalProjects/s2007/blh36_cdl28_dct23/blh36_cdl28_dct23/index.html">ECE476 Microcontrollers</a> project. On the completion of ECE491 we had created a Low Speed USB 2.0 stack library for the Atmel Mega32 Microcontroller using ~$6 worth of hardware and ~6000 standard lines of C.</p>
<p>Everybody in ECE476 used the CodeVisionAVR IDE, but we were a unique group in using avr-gcc. Though most students were okay with it, there were some minor features missing from the CodeVision compiler at the time, such as the ability to allocate objects on the heap. ;)</p>
<p>We rewrote the ECE476 code base in ECE491, again using avr-gcc, because we realized that the USB protocol was a lot more complex than the stack we had originally written. One of my main gripes in ECE491 was that I was writing highly object oriented code in a language which didn&#8217;t support any of the syntax. I&#8217;m starting to hack on the code base again, and a port to C++ seems like a good idea (since avr-g++ is also available), but there are some significant trade-offs running through my mind.</p>
<p><strong>The Trade-offs</strong></p>
<p>Things I want from C++ in the project:</p>
<ul>
<li>Namespaces &#8212; I hate worrying about global namespace issues. Though I&#8217;m not a huge fan of C++&#8217;s namespacing implementation, I&#8217;ll sure take it over <em>no </em>namespaces. :)</li>
<li>Templates &#8212; Casting to and from <tt>void *</tt>s in &#8220;containers&#8221; and haphazardly faking (efficient) tuples with <tt>void **</tt>s is <em>extremely</em> dangerous and causes really annoying bugs.</li>
<li>Class syntax &#8212; It&#8217;s ugly faking object orientation in C. Supported syntax is important to me because writing object oriented code in C &#8220;requires&#8221; you to prefix every function with a class name, like so:

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> data_packet_recalculate_crc16<span style="color: #009900;">&#40;</span>data_packet_t self<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> ... <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Plus, cool tools like <a href="http://www.stack.nl/~dimitri/doxygen/" target="_blank">doxygen</a> don&#8217;t pick up on the fact you&#8217;re writing in an object oriented style and adjust output accordingly (not that I&#8217;d expect them to).</li>
<li>Exceptions &#8212; If you&#8217;ve ever written a large amount of C code, you learn how precious basic exceptions are. <tt>goto</tt>s and cleanup code tend to get old after a short period of time.</li>
<li>Default arguments &#8212; I&#8217;m a fan of default arguments since it cuts down on the number of wrapper functions you have to write and maintain (though <a href="http://docs.python.org/tut/node6.html#SECTION006720000000000000000" target="_blank">keyword arguments</a> are even cooler :).</li>
</ul>
<p>Things I don&#8217;t want from C++ in the project:</p>
<ul>
<li>Larger binaries &#8212; The Atmel ATMega32 has 32KB of memory to fit my entire software stack, which I&#8217;ve found to be extremely confining, even with my C implementation (which manages reuse through <tt>void *</tt>s). The fact that templates cause the compiler to replicate code <a href="http://www.urbandictionary.com/define.php?term=sad+panda" target="_blank">makes me worry</a>.</li>
<li>Don&#8217;t really care, but maybe worth mentioning: vtables &#8212; I want inheritance with vptrs because it makes maintenance significantly easier for the straightforward class hierarchies; however, 16MHz chips are slow. Damn slow. Having vtables for some of the most primitive data structures seems ominous; however, I&#8217;m pretty sure that performance is a lot lower on the totem pole than maintainability in this instance.</li>
</ul>
<p><strong>The First Google Hit Says&#8230;</strong></p>
<p>I&#8217;ve read through <a href="http://www.diamand.org/bloat.html" target="_blank">Reducing C++ Code Bloat</a> and found it thought provoking. Though the article writes about gcc 3.4 and I&#8217;m using gcc 4.2, I can&#8217;t imagine that the underlying code-bloat concepts have changed much. I&#8217;m betting a lot of the compiler directive advice is taken care of by gcc&#8217;s -Os, but I&#8217;ll make a note to check it out.</p>
<p>It seems sensible to give up on exceptions ahead of time, but there seems to be some hope that the compiler can figure out good code reuse for the templates. I&#8217;m compiling to ELF, then performing and objcopy to turn it into Intel Hex object format &#8212; I&#8217;m hoping that the conversion is trivial and the good ELF compilation referenced in the article will stick.</p>
<p>In the end it seems like I&#8217;m just gambling on how much template reuse will occur. I sure hope that if I do all the porting-to-C++ work it optimizes well &#8212; <a href="http://groups.google.com/group/comp.lang.c++.moderated/browse_thread/thread/2b00649a935997f5/5feec243078c5fd8" target="_blank">template hoisting</a> looks like one of those idioms I&#8217;d prefer to leave alone. :(</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cdleary.com/2008/07/thoughts-on-cpp-in-small-memory-footprint-embedded-development/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
