<?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>therning.org/ magnus &#187; programming</title>
	<atom:link href="http://therning.org/magnus/archives/tag/programming/feed" rel="self" type="application/rss+xml" />
	<link>http://therning.org/magnus</link>
	<description>Incoherent mumblings</description>
	<lastBuildDate>Thu, 12 Jan 2012 13:40:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Are all languages leaky?</title>
		<link>http://therning.org/magnus/archives/321</link>
		<comments>http://therning.org/magnus/archives/321#comments</comments>
		<pubDate>Wed, 10 Oct 2007 16:59:31 +0000</pubDate>
		<dc:creator>Magnus</dc:creator>
				<category><![CDATA[Posts]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[thoughts]]></category>

		<guid isPermaLink="false">http://therning.org/magnus/archives/321</guid>
		<description><![CDATA[Dolio wrote an excellent comment on space leaks for the previous post. I&#8217;ve read posts on haskell-cafe before that mentions the concept but I&#8217;ve never bothered to ask. Thanks for clarifying the term! Dolio&#8217;s comment made me think of something I heard during a lecture on OO at university, with the risk of paraphrasing slightly: [...]]]></description>
			<content:encoded><![CDATA[<p>Dolio wrote an excellent comment on <em>space leaks</em> for the <a href="http://therning.org/magnus/archives/319#comment-82082">previous post</a>.  I&#8217;ve read posts on <em>haskell-cafe</em> before that mentions the concept but I&#8217;ve never bothered to ask.  Thanks for clarifying the term!</p>

<p>Dolio&#8217;s comment made me think of something I heard during a lecture on OO at university, with the risk of paraphrasing slightly:</p>

<blockquote>
  <p>It helps a lot to know how a C compiler works while programming C.</p>
</blockquote>

<p>In my mind this means that the language is &#8220;leaky&#8221; in a similar sense to how Joel&#8217;s <a href="http://www.joelonsoftware.com/articles/LeakyAbstractions.html">law of leaky abstractions</a>.  I suppose a programming language is little more than an abstraction of the machine underneath or, in the case of most languages, of the compiler/interpreter of the language.</p>

<p>Now of course I&#8217;m left wondering if &#8220;leakiness&#8221; can be measured, can languages be ordered based on it?  Maybe there are two dimensions to &#8220;leakiness&#8221;, how early you need knowledge of the layer below and how deep knowledge that knowledge has to be.  My gut tells me you can&#8217;t program in C for very long without needing some compiler knowledge but I&#8217;m not sure just how deep that knowledge needs to be.  My gut also tells me I can blissfully hack along in Haskell for quite a while before needing to know how things like laziness <em>actually</em> works, and again I don&#8217;t know if the knowledge needs to be deep or not<sup><a href="http://therning.org/magnus/archives/321#footnote_0_321" id="identifier_0_321" class="footnote-link footnote-identifier-link" title="My problem is really that I feel I know a fair bit about how compilers and interpreters work for imperative languages like C, while I&amp;#8217;m new to lazy functional languages like Haskell.">i</a></sup>.</p>

<p>Yes, I&#8217;ve been in a somewhat philosophical mood lately (some would say it&#8217;s a procrastinating mood <img src='http://therning.org/magnus/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ).</p>
<ol class="footnotes"><li id="footnote_0_321" class="footnote">My problem is really that I feel I know a fair bit about how compilers and interpreters work for imperative languages like C, while I&#8217;m new to lazy functional languages like Haskell.</li></ol><p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Ftherning.org%2Fmagnus%2Farchives%2F321&amp;title=Are%20all%20languages%20leaky%3F" id="wpa2a_2">Share/Bookmark</a></p>]]></content:encoded>
			<wfw:commentRss>http://therning.org/magnus/archives/321/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Types are funky&#8230;</title>
		<link>http://therning.org/magnus/archives/286</link>
		<comments>http://therning.org/magnus/archives/286#comments</comments>
		<pubDate>Thu, 17 May 2007 22:22:45 +0000</pubDate>
		<dc:creator>Magnus</dc:creator>
				<category><![CDATA[Posts]]></category>
		<category><![CDATA[haskell]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://therning.org/magnus/archives/286</guid>
		<description><![CDATA[I think I got this off a post on Haskell Planet a while back but didn&#8217;t watch it until today. I consider that to be 30 minutes well spent. It made me whish I had done some type theory at university.]]></description>
			<content:encoded><![CDATA[<p>I think I got this off a post on Haskell Planet a while back but didn&#8217;t watch it until today. I consider that to be 30 minutes well spent. It made me whish I had done some type theory at university.</p>

<p><embed style="width:400px; height:326px;" id="VideoPlayback" type="application/x-shockwave-flash" src="http://video.google.com/googleplayer.swf?docId=-4851250372422374791&#038;hl=en-GB" flashvars=""> </embed></p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Ftherning.org%2Fmagnus%2Farchives%2F286&amp;title=Types%20are%20funky%26%238230%3B" id="wpa2a_4">Share/Bookmark</a></p>]]></content:encoded>
			<wfw:commentRss>http://therning.org/magnus/archives/286/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>I am&#8230;</title>
		<link>http://therning.org/magnus/archives/282</link>
		<comments>http://therning.org/magnus/archives/282#comments</comments>
		<pubDate>Mon, 30 Apr 2007 08:42:12 +0000</pubDate>
		<dc:creator>Magnus</dc:creator>
				<category><![CDATA[Posts]]></category>
		<category><![CDATA[funny stuff]]></category>
		<category><![CDATA[personality test]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://therning.org/magnus/archives/282</guid>
		<description><![CDATA[I&#8217;m not too surprised by this actually&#8230; Which Programming Language are You?]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m not too surprised by this actually&#8230;</p>

<p><a href="http://www.bbspot.com/News/2006/08/language_quiz.php"><img src="http://www.bbspot.com/Images/News_Features/2006/08/language/python.jpg" width="300" height="90"
border="0" alt="You are Python You are slower than others, but easier to understand. You are a minimalist, who doesn't like clutter."/><br />Which Programming Language are You?</a></p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Ftherning.org%2Fmagnus%2Farchives%2F282&amp;title=I%20am%26%238230%3B" id="wpa2a_6">Share/Bookmark</a></p>]]></content:encoded>
			<wfw:commentRss>http://therning.org/magnus/archives/282/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PaiMei on Python 2.5</title>
		<link>http://therning.org/magnus/archives/278</link>
		<comments>http://therning.org/magnus/archives/278#comments</comments>
		<pubDate>Mon, 23 Apr 2007 14:04:31 +0000</pubDate>
		<dc:creator>Magnus</dc:creator>
				<category><![CDATA[Posts]]></category>
		<category><![CDATA[paimei]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://therning.org/magnus/archives/278</guid>
		<description><![CDATA[The PaiMei page says that you need Python 2.4, which turns out to be true due to it shipping with a compiled for Python 2.4 version of pydasm. Of course it&#8217;s possible to compile pydasm yourself, it&#8217;s even fairly easy just as long as you have the correct version of Visual Studio installed. You could [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://www.openrce.org/downloads/details/208/PaiMei">PaiMei</a> page says that you need Python 2.4, which turns out to be true due to it shipping with a compiled for Python 2.4 version of <code>pydasm</code>.  Of course it&#8217;s possible to compile <code>pydasm</code> yourself, it&#8217;s even fairly easy just as long as you have the correct version of Visual Studio installed. You could also use the utterly unofficial build I&#8217;ve made available <a href="http://therning.org/magnus_files/pydasm-1.2.win32-py2.5.exe">here</a>. Not even I know if I&#8217;m to be trusted though <img src='http://therning.org/magnus/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> . Use at own risk, and all that.</p>

<p>So, the steps are:</p>

<ol>
<li>Install Python 2.5 off <a href="http://www.python.org">python.org</a></li>
<li>Install <code>pydasm</code></li>
<li>Install PaiMei</li>
<li>Remove PaiMei&#8217;s version of pydasm (<code>c:\python25\site-packages\paimei\pydasm.pyd</code>) to be sure the correct one is used.</li>
</ol>

<p>Oh, I probably should say that I&#8217;ve only been using the core functionality of PaiMei (<code>pydbg</code> and <code>pydbg_core</code>). There may be other dependencies on Python 2.4 in PaiMei that I haven&#8217;t stumbled upon!</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Ftherning.org%2Fmagnus%2Farchives%2F278&amp;title=PaiMei%20on%20Python%202.5" id="wpa2a_8">Share/Bookmark</a></p>]]></content:encoded>
			<wfw:commentRss>http://therning.org/magnus/archives/278/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Using dbghelp</title>
		<link>http://therning.org/magnus/archives/277</link>
		<comments>http://therning.org/magnus/archives/277#comments</comments>
		<pubDate>Mon, 16 Apr 2007 23:07:24 +0000</pubDate>
		<dc:creator>Magnus</dc:creator>
				<category><![CDATA[Posts]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://therning.org/magnus/archives/277</guid>
		<description><![CDATA[I was disappointed to find the Debugging Tools for Windows contains precious few code examples that helps in understanding how to use the API. Especially if one is interested in using a symbol server to get symbols. Here are my findings: Make sure to use the correct version of dbhelp.{dll,h}. The ones that ship with [...]]]></description>
			<content:encoded><![CDATA[<p>I was disappointed to find the <a href="http://www.microsoft.com/whdc/devtools/debugging/default.mspx">Debugging Tools for Windows</a> contains precious few code examples that helps in understanding how to use the API. Especially if one is interested in using a symbol server to get symbols. Here are my findings:</p>

<ol>
<li><p>Make sure to use the correct version of <code>dbhelp.{dll,h}</code>. The ones that ship with Visual Studio or the Platform SDK aren&#8217;t the same as the one in the Debugging Tools (they lack quite a few functions).</p></li>
<li><p><a href="http://msdn2.microsoft.com/en-us/library/ms681368.aspx">SymSetSearchPath</a> doesn&#8217;t seem to accept the same syntax as <code>_NT_SYMBOL_PATH</code>. I gave up on using the former and resigned to having to set the latter in the environment.</p></li>
<li><p>You want to use <a href="http://msdn2.microsoft.com/en-us/library/ms681370.aspx">SymSrvGetFileIndexes</a> to find <code>id</code>, <code>two</code> and <code>three</code> that you pass to <a href="http://msdn2.microsoft.com/en-us/library/ms680726.aspx">SymFindFileInPath</a>.</p></li>
<li><p>Microsoft&#8217;s public symbol server only offers <em>public symbols</em>, what this means is that your <a href="http://msdn2.microsoft.com/en-us/library/ms680720.aspx">SymEnumSymbolsProc</a> will recieve <code>SYMBOL_INFO</code> structs with <code>Tag</code> set to <code>SymTagPublicSymbol</code> and <code>Flags</code> set to 0. Not very helpful if you want to find all functions in a DLL. <img src='http://therning.org/magnus/wp-includes/images/smilies/icon_sad.gif' alt=':-(' class='wp-smiley' /> </p></li>
</ol>

<p><em>Added 2007-04-17 / 09:13</em></p>

<p>You also want to use the following little snippet to include <code>dbghelp.h</code>:</p>

<pre><code>#define __out_xcount(x)
#define _NO_CVCONST_H
#include &lt;dbghelp.h&gt; 
</code></pre>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Ftherning.org%2Fmagnus%2Farchives%2F277&amp;title=Using%20dbghelp" id="wpa2a_10">Share/Bookmark</a></p>]]></content:encoded>
			<wfw:commentRss>http://therning.org/magnus/archives/277/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>On Windows programming</title>
		<link>http://therning.org/magnus/archives/276</link>
		<comments>http://therning.org/magnus/archives/276#comments</comments>
		<pubDate>Mon, 16 Apr 2007 22:46:49 +0000</pubDate>
		<dc:creator>Magnus</dc:creator>
				<category><![CDATA[Posts]]></category>
		<category><![CDATA[frustration]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://therning.org/magnus/archives/276</guid>
		<description><![CDATA[I always find myself going through the same motions when trying to programming on Windows: excitement, bewilderedness, frustration, relief. It&#8217;s exciting to find new libraries and frameworks that seem to deliver exactly the functionality I require. I feel bewildered because I don&#8217;t think I&#8217;ve ever come across a Windows C/C++ API that immediately makes sense [...]]]></description>
			<content:encoded><![CDATA[<p>I always find myself going through the same motions when trying to programming on Windows: excitement, bewilderedness, frustration, relief. It&#8217;s exciting to find new libraries and frameworks that seem to deliver exactly the functionality I require. I feel bewildered because I don&#8217;t think I&#8217;ve ever come across a Windows C/C++ API that immediately makes sense to me. Then follows a time of frustration, often a rather long period too, when I try to use the library/framework to solve the problem I have. At some point I hit that stage where my little project is debugged into behaving properly<sup><a href="http://therning.org/magnus/archives/276#footnote_0_276" id="identifier_0_276" class="footnote-link footnote-identifier-link" title="Through experience I&amp;#8217;ve come to the conclusion that it isn&amp;#8217;t worth the time and effort to try to fit Microsoft solutions into some logical framework. I&amp;#8217;d argue that&amp;#8217;s true for most closed-source solutions.">i</a></sup> and a sense of relief comes over me.</p>

<p>One thing that never ceases to amaze me is how many small surprising things there are lurking just under the hood in Windows:</p>

<ol>
<li><p>Want to print an error message? <a href="http://msdn2.microsoft.com/en-us/library/ms679360.aspx">GetLastError</a> gives you an error value and <a href="http://msdn2.microsoft.com/en-us/library/ms679351.aspx">FormatMessage</a> whips it into a nice printable string. Take a good long look at <code>FormatMessage</code>. Where is the convenience function a lÃ¡ <code>strerror</code>?</p></li>
<li><p>Another thing is the surprising <a href="http://msdn2.microsoft.com/en-us/library/ms682586.aspx">order of paths that is searched for DLLs</a>. By putting <code>PATH</code> so far down the list and completely leaving out an equivalent of <code>LD_LIBRARY_PATH</code> they actively encourage developers to copy DLLs into the home dirs of executables. I suspect this is inevitable given the DLL-hell phenomenon on Windows. It&#8217;s nonetheless extremely irritating when developing against a non-standard DLL (i.e. one that isn&#8217;t installed in <code>\windows\system32</code>).</p></li>
<li><p>The utter confusion I experience when trying to figure out just where to find the correct framework to use. There is considerable overlap between Visual Studio and the Windows Platform SDK. To add more confusion there are sometimes other frameworks that overlap the both of them, e.g. Debugging Tools for Windows provides <code>dbghelp.{dll,h}</code>, both of which are provided in slightly different versions in the other places<sup><a href="http://therning.org/magnus/archives/276#footnote_1_276" id="identifier_1_276" class="footnote-link footnote-identifier-link" title="A tip, make sure to use the ones that comes from Debugging Tools for Windows!">ii</a></sup>.</p></li>
<li><p>The lack of fixes for known issues, e.g. the version of <code>dbghelp.h</code> included in Debugging Tools for Windows can&#8217;t be included as is because it lacks the definition of a macro. The webpage announcing version 6.6.7.5 was updated 18 July, 2006. One would think that gives Microsoft ample time to address the issue, but no such luck.</p></li>
</ol>

<p>Well, that&#8217;s enough of ranting for one night&#8230;</p>
<ol class="footnotes"><li id="footnote_0_276" class="footnote">Through experience I&#8217;ve come to the conclusion that it isn&#8217;t worth the time and effort to try to fit Microsoft solutions into some logical framework. I&#8217;d argue that&#8217;s true for most closed-source solutions.</li><li id="footnote_1_276" class="footnote">A tip, make sure to use the ones that comes from Debugging Tools for Windows!</li></ol><p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Ftherning.org%2Fmagnus%2Farchives%2F276&amp;title=On%20Windows%20programming" id="wpa2a_12">Share/Bookmark</a></p>]]></content:encoded>
			<wfw:commentRss>http://therning.org/magnus/archives/276/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>My programmer personality</title>
		<link>http://therning.org/magnus/archives/262</link>
		<comments>http://therning.org/magnus/archives/262#comments</comments>
		<pubDate>Sun, 11 Mar 2007 23:12:14 +0000</pubDate>
		<dc:creator>Magnus</dc:creator>
				<category><![CDATA[Posts]]></category>
		<category><![CDATA[funny stuff]]></category>
		<category><![CDATA[personality test]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://therning.org/magnus/archives/262</guid>
		<description><![CDATA[I did the Programmer personality test: Your programmer personality type is: &#160;&#160;&#160;PHTB You&#8217;re a Planner. You may be slow, but you&#8217;ll usually find the best solution. If something&#8217;s worth doing, it&#8217;s worth doing right. You like coding at a High level. The world is made up of objects and components, you should create your programs [...]]]></description>
			<content:encoded><![CDATA[<p>I did the <a href="http://www.doolwind.com/index.php?page=11">Programmer personality test</a>:</p>

<blockquote>
  <p>Your programmer personality type is:</p>
  
  <p>&nbsp;&nbsp;&nbsp;<b><font size='5'>PHTB</font></b><br /></p>
  
  <p><b>You&#8217;re a <font size='5'>P</font>lanner.</b></p>
  
  <p>You may be slow, but you&#8217;ll usually find the best solution. If something&#8217;s
  worth doing, it&#8217;s worth doing right.</p>
  
  <p><b>You like coding at a <font size='5'>H</font>igh level.</b></p>
  
  <p>The world is made up of objects and components, you should create your
  programs in the same way.</p>
  
  <p><b>You work best in a <font size='5'>T</font>eam.</b></p>
  
  <p>A good group is better than the sum of it&#8217;s parts. The only thing better than
  a genius programmer is a cohesive group of genius programmers.</p>
  
  <p><b>You are a li<font size='5'>B</font>eral programmer.</b></p>
  
  <p>Programming is a complex task and you should use white space and comments as
  freely as possible to help simplify the task. We&#8217;re not writing on paper
  anymore so we can take up as much room as we need.</p>
</blockquote>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Ftherning.org%2Fmagnus%2Farchives%2F262&amp;title=My%20programmer%20personality" id="wpa2a_14">Share/Bookmark</a></p>]]></content:encoded>
			<wfw:commentRss>http://therning.org/magnus/archives/262/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Venting on C++</title>
		<link>http://therning.org/magnus/archives/253</link>
		<comments>http://therning.org/magnus/archives/253#comments</comments>
		<pubDate>Thu, 08 Feb 2007 00:06:48 +0000</pubDate>
		<dc:creator>Magnus</dc:creator>
				<category><![CDATA[Posts]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://therning.org/magnus/archives/253</guid>
		<description><![CDATA[Lately I&#8217;ve used C++ for a tool I&#8217;ve been working on at the office. Here are some idiosyncrasies in the standard library that I&#8217;ve noticed: I was expecting the following code to compile: std::string f("hello.txt"); std::ifstream in_file(f); But oh no! For some reason ifstream doesn&#8217;t have a constructor that accepts a standard C++ string. Instead [...]]]></description>
			<content:encoded><![CDATA[<p>Lately I&#8217;ve used C++ for a tool I&#8217;ve been working on at the office. Here are some idiosyncrasies in the standard library that I&#8217;ve noticed:</p>

<p>I was expecting the following code to compile:</p>

<pre><code>std::string f("hello.txt");
std::ifstream in_file(f);
</code></pre>

<p>But oh no! For some reason <code>ifstream</code> doesn&#8217;t have a constructor that accepts <em>a standard C++ string</em>. Instead I am forced to pass a <code>const char *</code> to the constructor, e.g.:</p>

<pre><code>std::string f("hello.txt");
std::ifstream in_file(f.c_str());
</code></pre>

<p>I guess that&#8217;s an excellent exmple of standardisation-by-committee where one part of the committee doesn&#8217;t have a clue what the other one is doing. Pathetic, really!</p>

<p>Now more stuff on <code>ifstream</code>. One would hope that opening a non-existing file for reading would trigger some sort of exception, at the very least reading from a non-existing file shouldn&#8217;t succeed. Oh, but not so in the wonderful world of C++. The following code compiles and executes just fine even if the file (<code>hello.txt</code>) doesn&#8217;t exist:</p>

<pre><code>std::ifstream inf("hello.txt");
std::string r;
inf &gt;&gt; r;
std::cout &lt;&lt; r &lt;&lt; std::endl;
</code></pre>

<p><code>r</code> is unmodified by <code>inf &gt;&gt; r;</code>. Even more interestingly <code>inf.eof()</code> returns <code>false</code>. Interestingly <code>inf.bad()</code> returns <code>false</code> too. Luckily <code>inf.good()</code><sup><a href="http://therning.org/magnus/archives/253#footnote_0_253" id="identifier_0_253" class="footnote-link footnote-identifier-link" title="I can&amp;#8217;t understand the reasonning behind having two methods, good() and bad() which aren&amp;#8217;t each other&amp;#8217;s opposites, their names certainly suggests they ought to be! It seems they are badly named, they should be called no_error_has_occured() and no_exceptional_failure_has_occured() respectively.">i</a></sup> returns <code>false</code> too, so there is some way of finding out that not all is well with <code>inf</code>. It was also pointed out that evaluating <code>inf</code> itself, such as in <code>if(inf) ...</code>, also results in <code>false</code>.</p>

<p>Ah, now I feel much better <img src='http://therning.org/magnus/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<ol class="footnotes"><li id="footnote_0_253" class="footnote">I can&#8217;t understand the reasonning behind having two methods, <code>good()</code> and <code>bad()</code> which aren&#8217;t each other&#8217;s opposites, their names certainly suggests they ought to be! It seems they are badly named, they should be called <code>no_error_has_occured()</code> and <code>no_exceptional_failure_has_occured()</code> respectively.</li></ol><p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Ftherning.org%2Fmagnus%2Farchives%2F253&amp;title=Venting%20on%20C%2B%2B" id="wpa2a_16">Share/Bookmark</a></p>]]></content:encoded>
			<wfw:commentRss>http://therning.org/magnus/archives/253/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>From the list of idiotic function signatures&#8230;</title>
		<link>http://therning.org/magnus/archives/157</link>
		<comments>http://therning.org/magnus/archives/157#comments</comments>
		<pubDate>Fri, 14 Jul 2006 22:53:34 +0000</pubDate>
		<dc:creator>Magnus</dc:creator>
				<category><![CDATA[Posts]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://therning.org/magnus/archives/157</guid>
		<description><![CDATA[I stumbled on the function QueryWorkingSet today. It seems to be usable for solving a problem I have&#8230; What&#8217;s really amazing about this function is its totally braindead signature: BOOL QueryWorkingSet( HANDLE hProcess, PVOID pv, DWORD cb ); Please look beyond the weird Microsoft-isms with type names and the idiotic Hungarian notation. What does it [...]]]></description>
			<content:encoded><![CDATA[<p>I stumbled on the function <code>QueryWorkingSet</code> today. It seems to be usable for solving a problem I have&#8230; What&#8217;s really amazing about this function is its totally braindead signature:</p>

<pre><code>BOOL QueryWorkingSet(
  HANDLE hProcess,
  PVOID pv,
  DWORD cb
);
</code></pre>

<p>Please look beyond the weird Microsoft-isms with type names and the idiotic Hungarian notation. What does it do? Basically it takes a buffer (<code>pv</code>) of a certain length (<code>cb</code>) and tries to stuff some information in it. So, how big do we have to make the buffer? Who knows?</p>

<p>Microsoft seems to have a desire to force programmers to make 2 calls to each function that returns data in a buffer. Functions don&#8217;t take an <code>int</code> as the bufffer&#8217;s length, instead they take <code>int *</code>. The first call would pass in 0 as length (or rather a pointer to a variable that&#8217;s set to 0). The call would fail, but the value of the variable would be set to the required size of the buffer for a successful call.</p>

<p>So, back to <code>QueryWorkingSet</code>. How does one find out the required size of the buffer <code>pv</code>? The only way seems to be repeated calls with progressively larger buffers. Brilliant API Microsoft!</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Ftherning.org%2Fmagnus%2Farchives%2F157&amp;title=From%20the%20list%20of%20idiotic%20function%20signatures%26%238230%3B" id="wpa2a_18">Share/Bookmark</a></p>]]></content:encoded>
			<wfw:commentRss>http://therning.org/magnus/archives/157/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Banging my head against sparse files</title>
		<link>http://therning.org/magnus/archives/156</link>
		<comments>http://therning.org/magnus/archives/156#comments</comments>
		<pubDate>Tue, 11 Jul 2006 23:22:21 +0000</pubDate>
		<dc:creator>Magnus</dc:creator>
				<category><![CDATA[Posts]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[sparse files]]></category>

		<guid isPermaLink="false">http://therning.org/magnus/archives/156</guid>
		<description><![CDATA[Lately I&#8217;ve had use for sparse files to solve a problem. Simple, yes. Use lseek and write (or read). Even better, at least in this case, use pwrite and pread. Should be simple, right? Yes, should be. I was banging my head against code equivalent to the following for quite a few hours: #define _XOPEN_SOURCE [...]]]></description>
			<content:encoded><![CDATA[<p>Lately I&#8217;ve had use for sparse files to solve a problem. Simple, yes. Use <code>lseek</code> and <code>write</code> (or <code>read</code>). Even better, at least in this case, use <code>pwrite</code> and <code>pread</code>. Should be simple, right? Yes, should be.</p>

<p>I was banging my head against code equivalent to the following for quite a few hours:</p>

<pre><code>#define _XOPEN_SOURCE 500
#define _FILE_OFFSET_BITS 64
#include &lt;errno.h&gt;
#include &lt;fcntl.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;sys/stat.h&gt;

char buf[1024];

int
main(int argc, char **argv)
{
    int n;
    off_t off;

    for(n = 0; n &lt; 1024; n++) {
        buf[n] = 'a';
    }

    int fd1;

    if(-1 == (fd1 = open("file1", O_WRONLY | O_CREAT, 0666)))
        error(EXIT_FAILURE, errno, "open (test1)");
    off = 0x80000000;
    printf("size: %d\n", sizeof(off));
    if(pwrite(fd1, buf, 1024, off) != 1024)
        error(EXIT_FAILURE, errno, "pwrite (%lli)", off);

    return(0);
}
</code></pre>

<p>Can you spot the problem?</p>

<p>Well, I can tell you it doesn&#8217;t work. <code>off_t</code> becomes 64-bit due to the <code>#define _FILE_OFFSET_BITS 64</code> so the <code>off</code> variable isn&#8217;t negative <em>in my code</em>. Somehow however it becomes negative on the way into <code>pwrite</code>, and without a single compiler or linker error at that!</p>

<p>Have you spot the problem yet?</p>

<p>Here&#8217;s the correctly working code:</p>

<pre><code>#define _XOPEN_SOURCE 500
#define _FILE_OFFSET_BITS 64
#include &lt;errno.h&gt;
#include &lt;fcntl.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;sys/stat.h&gt;
#include &lt;unistd.h&gt;

char buf[1024];

int
main(int argc, char **argv)
{
    int n;
    off_t off;

    for(n = 0; n &lt; 1024; n++) {
        buf[n] = 'a';
    }

    int fd1;

    if(-1 == (fd1 = open("file1", O_WRONLY | O_CREAT, 0666)))
        error(EXIT_FAILURE, errno, "open (test1)");
    off = 0x80000000;
    printf("size: %d\n", sizeof(off));
    if(pwrite(fd1, buf, 1024, off) != 1024)
        error(EXIT_FAILURE, errno, "pwrite (%lli)", off);

    return(0);
}
</code></pre>

<p>Don&#8217;t see any difference? Look at the list of included files!</p>

<p>Let me just say the C and I aren&#8217;t on speaking terms at the moment!</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Ftherning.org%2Fmagnus%2Farchives%2F156&amp;title=Banging%20my%20head%20against%20sparse%20files" id="wpa2a_20">Share/Bookmark</a></p>]]></content:encoded>
			<wfw:commentRss>http://therning.org/magnus/archives/156/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

