<?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: Closures In Straight C</title>
	<atom:link href="http://porkrind.org/missives/closures-in-straight-c/feed/" rel="self" type="application/rss+xml" />
	<link>http://porkrind.org/missives/closures-in-straight-c/</link>
	<description>Portal For The Masses</description>
	<lastBuildDate>Tue, 15 May 2012 02:15:56 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Anonymous</title>
		<link>http://porkrind.org/missives/closures-in-straight-c/comment-page-1/#comment-524</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Tue, 15 May 2012 02:15:56 +0000</pubDate>
		<guid isPermaLink="false">http://porkrind.org/missives2/?p=13#comment-524</guid>
		<description>Thanks, I too have found this useful in embedded environment. Beats having global or &lt;em&gt;static global&lt;/em&gt; variables. As others have pointed out, it may be quite far from closures as they are traditionally understood, but I wouldn&#039;t have found this article by googling &quot;closures in C&quot; if you didn&#039;t mention them. Ended up having code similar to this:

&lt;code&gt;

typedef struct
{
    int counter;
} context_t;


static void onEventCallback(void *event, void *context)
{
    context_t *self = (context_t *)context;
    self-&gt;counter++;
}


void processAsync()
{
    context_t context, *self = &amp;context;
    self-&gt;counter = 0;
    startProcess(onEventCallback, self);
}


&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>Thanks, I too have found this useful in embedded environment. Beats having global or <em>static global</em> variables. As others have pointed out, it may be quite far from closures as they are traditionally understood, but I wouldn&#8217;t have found this article by googling &#8220;closures in C&#8221; if you didn&#8217;t mention them. Ended up having code similar to this:</p>
<p><code></p>
<p>typedef struct<br />
{<br />
    int counter;<br />
} context_t;</p>
<p>static void onEventCallback(void *event, void *context)<br />
{<br />
    context_t *self = (context_t *)context;<br />
    self-&gt;counter++;<br />
}</p>
<p>void processAsync()<br />
{<br />
    context_t context, *self = &amp;context;<br />
    self-&gt;counter = 0;<br />
    startProcess(onEventCallback, self);<br />
}</p>
<p></code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jack</title>
		<link>http://porkrind.org/missives/closures-in-straight-c/comment-page-1/#comment-514</link>
		<dc:creator>jack</dc:creator>
		<pubDate>Tue, 21 Feb 2012 23:48:02 +0000</pubDate>
		<guid isPermaLink="false">http://porkrind.org/missives2/?p=13#comment-514</guid>
		<description>..and he scores!!!</description>
		<content:encoded><![CDATA[<p>..and he scores!!!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Travis Parks</title>
		<link>http://porkrind.org/missives/closures-in-straight-c/comment-page-1/#comment-513</link>
		<dc:creator>Travis Parks</dc:creator>
		<pubDate>Tue, 21 Feb 2012 13:35:44 +0000</pubDate>
		<guid isPermaLink="false">http://porkrind.org/missives2/?p=13#comment-513</guid>
		<description>I actually found this article very useful. I have been working in Node.js lately and realized that it&#039;s asynchronous approach could actually simplify some error handling aspects in C. The only problem: closures. However, following your lead, as long as every function has a &quot;context&quot; object, you can overcome that limitation. Sure, the developer has to allocate/deallocate memory, but I don&#039;t think this is &quot;inconvenient&quot;. Simply define a &quot;context&quot; object for each function, allocate that context for each &quot;instance&quot; of that function and pass it and the context as a callback. I may look into creating an async library in C, similar to &lt;a href=&quot;https://github.com/caolan/async&quot; rel=&quot;nofollow&quot;&gt;caolan/async&lt;/a&gt;, just to play around with the concept.</description>
		<content:encoded><![CDATA[<p>I actually found this article very useful. I have been working in Node.js lately and realized that it&#8217;s asynchronous approach could actually simplify some error handling aspects in C. The only problem: closures. However, following your lead, as long as every function has a &#8220;context&#8221; object, you can overcome that limitation. Sure, the developer has to allocate/deallocate memory, but I don&#8217;t think this is &#8220;inconvenient&#8221;. Simply define a &#8220;context&#8221; object for each function, allocate that context for each &#8220;instance&#8221; of that function and pass it and the context as a callback. I may look into creating an async library in C, similar to <a href="https://github.com/caolan/async" rel="nofollow">caolan/async</a>, just to play around with the concept.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: hopia</title>
		<link>http://porkrind.org/missives/closures-in-straight-c/comment-page-1/#comment-485</link>
		<dc:creator>hopia</dc:creator>
		<pubDate>Wed, 14 Dec 2011 17:25:33 +0000</pubDate>
		<guid isPermaLink="false">http://porkrind.org/missives2/?p=13#comment-485</guid>
		<description>As a long time C developer, I totally agree that callbacks and cookies are the closest thing to closures in the C world.  Of course, we all know it&#039;s not part of the language, so it&#039;s not as intuitive and natural to use.  However, I think the point of the article is that C developers can and have been able to &quot;simulate&quot; the behavior.</description>
		<content:encoded><![CDATA[<p>As a long time C developer, I totally agree that callbacks and cookies are the closest thing to closures in the C world.  Of course, we all know it&#8217;s not part of the language, so it&#8217;s not as intuitive and natural to use.  However, I think the point of the article is that C developers can and have been able to &#8220;simulate&#8221; the behavior.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David</title>
		<link>http://porkrind.org/missives/closures-in-straight-c/comment-page-1/#comment-18</link>
		<dc:creator>David</dc:creator>
		<pubDate>Sun, 11 Jun 2006 20:39:35 +0000</pubDate>
		<guid isPermaLink="false">http://porkrind.org/missives2/?p=13#comment-18</guid>
		<description>&lt;p&gt;Douglas, I&#8217;m not sure what you mean by &#8220;everything&#8221;. Please explain.&lt;/p&gt;
&lt;p&gt;You could keep a pointer to you local variables (if you were sure the callback were going to happen before they go away) or you could use the variables in the created structure instead of locals (this is what C folk tend to do in these cases).&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Douglas, I&#8217;m not sure what you mean by &#8220;everything&#8221;. Please explain.</p>
<p>You could keep a pointer to you local variables (if you were sure the callback were going to happen before they go away) or you could use the variables in the created structure instead of locals (this is what C folk tend to do in these cases).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Douglas</title>
		<link>http://porkrind.org/missives/closures-in-straight-c/comment-page-1/#comment-17</link>
		<dc:creator>Douglas</dc:creator>
		<pubDate>Sat, 10 Jun 2006 12:13:16 +0000</pubDate>
		<guid isPermaLink="false">http://porkrind.org/missives2/?p=13#comment-17</guid>
		<description>&lt;p&gt;The difference is the same as between &#8220;I want X, Y and Z&#8221; and &#8220;I want everything&#8221;. &lt;/p&gt;
&lt;p&gt;If you only want X, Y and Z in the first place, that&#8217;s going to be fine, it will work in that limited case. But I don&#8217;t know how you&#8217;d express &#8220;everything&#8221; in C.&lt;/p&gt;
&lt;p&gt;What you want is a pointer to the current scope, not a pointer to a copy of some of the variables from the current scope.&lt;/p&gt;
&lt;p&gt;Douglas&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>The difference is the same as between &#8220;I want X, Y and Z&#8221; and &#8220;I want everything&#8221;. </p>
<p>If you only want X, Y and Z in the first place, that&#8217;s going to be fine, it will work in that limited case. But I don&#8217;t know how you&#8217;d express &#8220;everything&#8221; in C.</p>
<p>What you want is a pointer to the current scope, not a pointer to a copy of some of the variables from the current scope.</p>
<p>Douglas</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David</title>
		<link>http://porkrind.org/missives/closures-in-straight-c/comment-page-1/#comment-16</link>
		<dc:creator>David</dc:creator>
		<pubDate>Sat, 10 Jun 2006 10:07:00 +0000</pubDate>
		<guid isPermaLink="false">http://porkrind.org/missives2/?p=13#comment-16</guid>
		<description>&lt;p&gt;But you see, that is my point. The &#8220;closing over itself&#8221; is just syntactic sugar.&lt;/p&gt;
&lt;p&gt;C only supports 2 types of variables. Global variables and stack based local variables. Anything  more than that is supported by the standard library&#8212;malloc, to be specific.&lt;/p&gt;
&lt;p&gt;The point is that you have to hand hold C to get anything that lasts longer than a local stack variable but shorter than a global variable (which lasts forever).&lt;/p&gt;
&lt;p&gt;By passing these user data pointers around with the function pointer itself, you are effectively passing around your local lexical environment&#8212;just the parts you care about.&lt;/p&gt;
&lt;p&gt;Like you guys pointed out (and as I said in the article), it&#8217;s painful to  do compared to a language with language level closures.&lt;/p&gt;
&lt;p&gt;But that&#8217;s the way it is with C for practically everything! It&#8217;s the exact same concept as a closure, regardless of how much extra you have to do to implement it&#8230;&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>But you see, that is my point. The &#8220;closing over itself&#8221; is just syntactic sugar.</p>
<p>C only supports 2 types of variables. Global variables and stack based local variables. Anything  more than that is supported by the standard library&#8212;malloc, to be specific.</p>
<p>The point is that you have to hand hold C to get anything that lasts longer than a local stack variable but shorter than a global variable (which lasts forever).</p>
<p>By passing these user data pointers around with the function pointer itself, you are effectively passing around your local lexical environment&#8212;just the parts you care about.</p>
<p>Like you guys pointed out (and as I said in the article), it&#8217;s painful to  do compared to a language with language level closures.</p>
<p>But that&#8217;s the way it is with C for practically everything! It&#8217;s the exact same concept as a closure, regardless of how much extra you have to do to implement it&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ryan Davis</title>
		<link>http://porkrind.org/missives/closures-in-straight-c/comment-page-1/#comment-15</link>
		<dc:creator>Ryan Davis</dc:creator>
		<pubDate>Sat, 10 Jun 2006 08:37:34 +0000</pubDate>
		<guid isPermaLink="false">http://porkrind.org/missives2/?p=13#comment-15</guid>
		<description>&lt;p&gt;Thirded. Not even close to closures.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Thirded. Not even close to closures.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Blake Watters</title>
		<link>http://porkrind.org/missives/closures-in-straight-c/comment-page-1/#comment-14</link>
		<dc:creator>Blake Watters</dc:creator>
		<pubDate>Sat, 10 Jun 2006 08:33:19 +0000</pubDate>
		<guid isPermaLink="false">http://porkrind.org/missives2/?p=13#comment-14</guid>
		<description>&lt;p&gt;Those are just pointers to data. A closure has to &#8216;close over&#8217; its lexical scope, restoring its state and scope at run time.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Those are just pointers to data. A closure has to &#8216;close over&#8217; its lexical scope, restoring its state and scope at run time.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ratatask</title>
		<link>http://porkrind.org/missives/closures-in-straight-c/comment-page-1/#comment-13</link>
		<dc:creator>ratatask</dc:creator>
		<pubDate>Sat, 10 Jun 2006 07:41:14 +0000</pubDate>
		<guid isPermaLink="false">http://porkrind.org/missives2/?p=13#comment-13</guid>
		<description>&lt;p&gt;This isn&#8217;t really closure &#8211; you have to manually create the environment you want to capture (your struct) and make it have dynamic extent(stuff it at file scope or malloc it).&lt;/p&gt;
&lt;p&gt;Extremly painful compared to languages with real closures.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>This isn&#8217;t really closure &#8211; you have to manually create the environment you want to capture (your struct) and make it have dynamic extent(stuff it at file scope or malloc it).</p>
<p>Extremly painful compared to languages with real closures.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
<!-- This Quick Cache file was built for (  porkrind.org/missives/closures-in-straight-c/feed/ ) in 0.13866 seconds, on May 21st, 2012 at 4:19 pm UTC. -->
<!-- This Quick Cache file will automatically expire ( and be re-built automatically ) on May 21st, 2012 at 5:19 pm UTC -->
