    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: Letter to the Editor</title>
        <link>https://members.accu.org/index.php/articles/2755</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>




<div class="xar-mod-head"><span class="xar-mod-title">Letters to the Editor + CVu Journal Vol 32, #1 - March 2020</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c184/">Journal Columns</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c186/">LettersEditor</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c77/">CVu</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c408/">321</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/c186-408/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/articles/c186+408/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;Letter to the Editor</h1>
<p><strong>Author:</strong>&nbsp;Bob Schmidt</p>
<p>
<strong>Date:</strong> 01 March 2020 21:59:40 +00:00 or Sun, 01 March 2020 21:59:40 +00:00</p>
<p><strong>Summary:</strong>&nbsp;Silas S. Brown responds to Ian Bruntlettâ€™s article.</p>
<p><strong>Body:</strong>&nbsp;<p>Dear Editor,</p>

<p>I enjoyed Ian Bruntlettâ€™s article â€˜Static C library and GNU Makeâ€™ (<em>C Vu</em> 31.6, January 2020), but I hope he wonâ€™t mind my pointing out some improvements that can be made to the code, in the spirit of ACCU being a good learning environment.</p>

<p>Firstly, Iâ€™m afraid there is a bug in Listing 2, specifically this part:</p>

<pre class="programlisting">
  for (int rhs = length-1;
    isspace(text[rhs]);
    --rhs
    )
    text[rhs] = '\0';</pre>
	
<p>Consider what happens when <code>text</code> is a string consisting entirely of spaces and nothing else. In this case the loop decrementing <code>rhs</code> will quite happily set it to <code>-1</code> and try to read it for <code>isspace()</code>. In most cases, the byte before the string starts will (a) be readable by your program and (b) not happen to contain a space, in which case nothing will happen. But if it does happen to contain a space, then weâ€™re looking at memory corruption or, in the unlikely event that its address is off-limits to your program, youâ€™ll get a segmentation fault.</p>

<p>The easiest fix would be to add a condition for <code>rhs &gt;= 0</code> in the loop. But if we start using <code>size_t</code> instead of <code>int</code> (which weâ€™d need to do if we want to support strings that are so enormous their lengths cannot be represented by a signed integer on the platform) then weâ€™d have to explicitly write <code>if (rhs==0) break;</code> at the end of the loop body.</p>

<p>Incidentally, it is not really necessary to overwrite <em>every</em> space with <code>\0</code> when right-trimming; only the first one needs to be overwritten (and this might make a speed difference if memory-writes are slower than memory-reads on the system). So it might be better to write the loop as:</p>

<pre class="programlisting">
  size_t rhs;
  for (rhs=length; rhs &amp;&amp; isspace(text[rhs-1]);
       rhs--);
  text[rhs] = '\0';</pre>
  
<p>and while weâ€™re making such improvements, <code>ltrim()</code> might as well use <code>memmove()</code> rather than writing the loop manually (the library <code>memmove</code> will likely have optimisations that can move multiple bytes at a time using the CPUâ€™s extended registers), and the two <code>ltrimcpy</code> and <code>rtrimcpy</code> functions can be improved to copy only the non-whitespace portion of the string (after all, thereâ€™s no point in copying the spaces only to remove them). But on the other hand, programmer time is also a resource and sometimes we shouldnâ€™t use our time optimising things that donâ€™t really need to be optimal; Iâ€™m mentioning this only for completeness.</p>

<p>I realise the thrust of the article is about Makefiles and not really about the code itself, but still thought the above might be worth pointing out for the sake of the learners.</p>

<p>Many thanks.</p>

<p>Silas</p>

<p class="bio"><span class="author"><b>Silas S. Brown</b></span> is a partially-sighted Computer Science post-doc in Cambridge who currently works in part-time assistant tuition and part-time for Oracle. He has been an ACCU member since 1994.</p>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
