<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Efficient C Tips #8 &#8211; Use const</title>
	<atom:link href="http://embeddedgurus.com/stack-overflow/2009/03/efficient-c-tips-8-use-const/feed/" rel="self" type="application/rss+xml" />
	<link>http://embeddedgurus.com/stack-overflow/2009/03/efficient-c-tips-8-use-const/</link>
	<description>Thoughts on embedded systems by Nigel Jones</description>
	<lastBuildDate>Sun, 06 May 2012 10:34:02 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Eric Miller</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/03/efficient-c-tips-8-use-const/comment-page-1/#comment-14426</link>
		<dc:creator>Eric Miller</dc:creator>
		<pubDate>Wed, 15 Feb 2012 11:18:08 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/03/30/efficient-c-tips-8-use-const/#comment-14426</guid>
		<description>Hi Lundlin,

You have a typo:

sum += ptr[i];

really compiles into something that looks like:

sum += *(ptr + i);

(your post omitted the pointer dereference)</description>
		<content:encoded><![CDATA[<p>Hi Lundlin,</p>
<p>You have a typo:</p>
<p>sum += ptr[i];</p>
<p>really compiles into something that looks like:</p>
<p>sum += *(ptr + i);</p>
<p>(your post omitted the pointer dereference)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lundin</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/03/efficient-c-tips-8-use-const/comment-page-1/#comment-2951</link>
		<dc:creator>Lundin</dc:creator>
		<pubDate>Wed, 15 Dec 2010 07:43:32 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/03/30/efficient-c-tips-8-use-const/#comment-2951</guid>
		<description>Maybe it does, maybe not :)

You&#039;re assuming 0-indexing. The problem is that you can&#039;t compare against any other value than zero when downcounting an unsigned int, therefore you must use a 1-indexed array. Or alternatively something ugly like this:

#define MSB_MASK_32 ( 1U &lt;&lt; 31 )

BOOL is_overflow (uint32 i)
{
  i = i &amp; (1U &lt; 0;
}

for(i=MAX-1; !is_overflow(i); i--)

I cannot quite see how this is a better alternative. These obfuscated examples are surely valid reasons to never write downcounting loops.</description>
		<content:encoded><![CDATA[<p>Maybe it does, maybe not <img src='http://embeddedgurus.com/stack-overflow/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>You&#8217;re assuming 0-indexing. The problem is that you can&#8217;t compare against any other value than zero when downcounting an unsigned int, therefore you must use a 1-indexed array. Or alternatively something ugly like this:</p>
<p>#define MSB_MASK_32 ( 1U &lt;&lt; 31 )</p>
<p>BOOL is_overflow (uint32 i)<br />
{<br />
  i = i &amp; (1U &lt; 0;<br />
}</p>
<p>for(i=MAX-1; !is_overflow(i); i&#8211;)</p>
<p>I cannot quite see how this is a better alternative. These obfuscated examples are surely valid reasons to never write downcounting loops.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anonymous</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/03/efficient-c-tips-8-use-const/comment-page-1/#comment-2945</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Tue, 14 Dec 2010 17:28:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/03/30/efficient-c-tips-8-use-const/#comment-2945</guid>
		<description>&gt;Lundin says:
&gt;December 3, 2010 at 10:59 am
&gt;Why? I don’t see why this would be different from pointer notation:
&gt;
&gt;for(i=MAX; i!=0; i–)
&gt;{
&gt;sum += ptr[i];
&gt;}
This code writes one element past the end of the ptr buffer, and does not touch the bottom (index=0) of the ptr buffer.  That is the problem with counting down using an index.  The terminating condition is harder to capture.</description>
		<content:encoded><![CDATA[<p>&gt;Lundin says:<br />
&gt;December 3, 2010 at 10:59 am<br />
&gt;Why? I don’t see why this would be different from pointer notation:<br />
&gt;<br />
&gt;for(i=MAX; i!=0; i–)<br />
&gt;{<br />
&gt;sum += ptr[i];<br />
&gt;}<br />
This code writes one element past the end of the ptr buffer, and does not touch the bottom (index=0) of the ptr buffer.  That is the problem with counting down using an index.  The terminating condition is harder to capture.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lundin</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/03/efficient-c-tips-8-use-const/comment-page-1/#comment-2874</link>
		<dc:creator>Lundin</dc:creator>
		<pubDate>Fri, 03 Dec 2010 15:59:35 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/03/30/efficient-c-tips-8-use-const/#comment-2874</guid>
		<description>Why? I don&#039;t see why this would be different from pointer notation:

for(i=MAX; i!=0; i--)
{
sum += ptr[i];
}

Also, ptr[i] is so-called &quot;syntatic sugar&quot;. Behind the lines, the compiler will actually read that code as

sum += ptr + i;

------

As I sidenote, I&#039;m against counting down myself. There is no doubt that down-counting loops are faster than up-counting ones on any known CPU. 

Still, I don&#039;t use them. In my opinion such optimizations should be handled by the compiler&#039;s optimizer. When chosing between readability and efficiency, readability should win in 99% of the cases.

------

Regarding &quot;Saks notation&quot;, I&#039;m sure he&#039;s got plenty of good arguments about where to place the const keyword. 

However, the C committee has stated that the feature of placing storage-class specifiers (extern, static etc) anywhere in the declaration will be removed from the language in the future.  Thus int static x; will not be allowed in the future. So for consistency reasons,  why not use the same notation for type qualifiers (const volatile etc)?</description>
		<content:encoded><![CDATA[<p>Why? I don&#8217;t see why this would be different from pointer notation:</p>
<p>for(i=MAX; i!=0; i&#8211;)<br />
{<br />
sum += ptr[i];<br />
}</p>
<p>Also, ptr[i] is so-called &#8220;syntatic sugar&#8221;. Behind the lines, the compiler will actually read that code as</p>
<p>sum += ptr + i;</p>
<p>&#8212;&#8212;</p>
<p>As I sidenote, I&#8217;m against counting down myself. There is no doubt that down-counting loops are faster than up-counting ones on any known CPU. </p>
<p>Still, I don&#8217;t use them. In my opinion such optimizations should be handled by the compiler&#8217;s optimizer. When chosing between readability and efficiency, readability should win in 99% of the cases.</p>
<p>&#8212;&#8212;</p>
<p>Regarding &#8220;Saks notation&#8221;, I&#8217;m sure he&#8217;s got plenty of good arguments about where to place the const keyword. </p>
<p>However, the C committee has stated that the feature of placing storage-class specifiers (extern, static etc) anywhere in the declaration will be removed from the language in the future.  Thus int static x; will not be allowed in the future. So for consistency reasons,  why not use the same notation for type qualifiers (const volatile etc)?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nigel Jones</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/03/efficient-c-tips-8-use-const/comment-page-1/#comment-2865</link>
		<dc:creator>Nigel Jones</dc:creator>
		<pubDate>Fri, 03 Dec 2010 13:23:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/03/30/efficient-c-tips-8-use-const/#comment-2865</guid>
		<description>I often will use the style you suggest. However doing so precludes counting down if you use index as the loop variable. As a result, I can often get faster code using the pointer notation.</description>
		<content:encoded><![CDATA[<p>I often will use the style you suggest. However doing so precludes counting down if you use index as the loop variable. As a result, I can often get faster code using the pointer notation.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peter</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/03/efficient-c-tips-8-use-const/comment-page-1/#comment-2848</link>
		<dc:creator>Peter</dc:creator>
		<pubDate>Fri, 03 Dec 2010 07:27:07 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/03/30/efficient-c-tips-8-use-const/#comment-2848</guid>
		<description>Hello Nigel,
Why don&#039;t you use this style:
sum += ptr[index]; 

Then you don&#039;t have to deal with pointer increment.

/Peter</description>
		<content:encoded><![CDATA[<p>Hello Nigel,<br />
Why don&#8217;t you use this style:<br />
sum += ptr[index]; </p>
<p>Then you don&#8217;t have to deal with pointer increment.</p>
<p>/Peter</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Engineer</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/03/efficient-c-tips-8-use-const/comment-page-1/#comment-180</link>
		<dc:creator>Engineer</dc:creator>
		<pubDate>Thu, 28 Jan 2010 08:34:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/03/30/efficient-c-tips-8-use-const/#comment-180</guid>
		<description>- ashleigh,it would be great then if we could have a return of your experience in such embedded systems, multiprocess multithread systems are really a nightmare for every programmer.Some good experienced directions would be greatly appreciated.Fahmi MEGDICHE</description>
		<content:encoded><![CDATA[<p>- ashleigh,it would be great then if we could have a return of your experience in such embedded systems, multiprocess multithread systems are really a nightmare for every programmer.Some good experienced directions would be greatly appreciated.Fahmi MEGDICHE</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ashleigh</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/03/efficient-c-tips-8-use-const/comment-page-1/#comment-179</link>
		<dc:creator>ashleigh</dc:creator>
		<pubDate>Tue, 26 Jan 2010 01:35:28 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/03/30/efficient-c-tips-8-use-const/#comment-179</guid>
		<description>Re Anonymous...Callbacks, and threads, are 2 areas that are tackling by the unwitting thinking its all terribly easy. Then they blame the compiler for the race conditions, stack overflows and deadlocks that result.Such people terrify me.I spend 10 years writing concurrent mult-thread-multi-task programs and I&#039;m well aware of the difficulties and pitfalls. Those who leap in get burned - every time. A place to go with great care.</description>
		<content:encoded><![CDATA[<p>Re Anonymous&#8230;Callbacks, and threads, are 2 areas that are tackling by the unwitting thinking its all terribly easy. Then they blame the compiler for the race conditions, stack overflows and deadlocks that result.Such people terrify me.I spend 10 years writing concurrent mult-thread-multi-task programs and I&#39;m well aware of the difficulties and pitfalls. Those who leap in get burned &#8211; every time. A place to go with great care.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nigel Jones</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/03/efficient-c-tips-8-use-const/comment-page-1/#comment-178</link>
		<dc:creator>Nigel Jones</dc:creator>
		<pubDate>Sun, 12 Apr 2009 12:51:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/03/30/efficient-c-tips-8-use-const/#comment-178</guid>
		<description>Hi Anonymous:I think I know what you are getting at. Since I&#039;m a big proponent of pointers to functions I think it&#039;s only fair that I tackle their dark side, so to speak. Look for a posting on this in the next week or two.</description>
		<content:encoded><![CDATA[<p>Hi Anonymous:I think I know what you are getting at. Since I&#8217;m a big proponent of pointers to functions I think it&#8217;s only fair that I tackle their dark side, so to speak. Look for a posting on this in the next week or two.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anonymous</title>
		<link>http://embeddedgurus.com/stack-overflow/2009/03/efficient-c-tips-8-use-const/comment-page-1/#comment-177</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Fri, 03 Apr 2009 02:12:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.gfcdev.org/test-stack/2009/03/30/efficient-c-tips-8-use-const/#comment-177</guid>
		<description>Dear Nigel, i religiously follow your blog (and advice too). I was wondering if you can write an article on your view of callbacks, plain simple C callback functions.I&#039;ve come across many people who use callback functions when it is not at all required. People often forget the fact the callbacks execute in the context of the calling task. In some cases that is how it should be done (like an application specific memory allocation routine) while in other cases its not the right thing to do.I believe people lack the art of defining a finite scope for the task, what data it operates on and how it should communicate with other tasks. That said, i am no authority over C and designing complex systems.I hope you got what i am aiming at. I am finding it hard to explain. Please, if you can spend your precious time sharing your experiences with callbacks, that would be godsend :-)Thanks!</description>
		<content:encoded><![CDATA[<p>Dear Nigel, i religiously follow your blog (and advice too). I was wondering if you can write an article on your view of callbacks, plain simple C callback functions.I&#8217;ve come across many people who use callback functions when it is not at all required. People often forget the fact the callbacks execute in the context of the calling task. In some cases that is how it should be done (like an application specific memory allocation routine) while in other cases its not the right thing to do.I believe people lack the art of defining a finite scope for the task, what data it operates on and how it should communicate with other tasks. That said, i am no authority over C and designing complex systems.I hope you got what i am aiming at. I am finding it hard to explain. Please, if you can spend your precious time sharing your experiences with callbacks, that would be godsend <img src='http://embeddedgurus.com/stack-overflow/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> Thanks!</p>
]]></content:encoded>
	</item>
</channel>
</rss>

