    <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  :: Francis' Scribbles</title>
        <link>https://members.accu.org/index.php/articles/831</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">Francis' Scribbles from CVu journal + CVu Journal Vol 17, #5 - Oct 2005</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/c181/">Francis' Scribbles</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/c94/">175</a>
<br />

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

                    -                        <a href="https://members.accu.org/index.php/articles/c181+94/">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;Francis' Scribbles</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 07 October 2005 05:00:00 +01:00 or Fri, 07 October 2005 05:00:00 +01:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e23" id="d0e23"></a></h2>
</div>
<p>Several years ago Gary Lancaster implemented my Playpen library.
The implementation worked well and did exactly what I wanted. We
fixed a couple of bugs and added a bit to it. One day I added my
own default palette, which I implemented by with a function. The
obvious place to call this was from within the constructor for
playpen. At that time, everything seemed to continue to work.</p>
<p>Then I noticed that on some machines the screen incorrectly
updated when a program first constructed a playpen instance. It was
not a big problem and seemed relatively harmless. I mentioned it to
Gary but he could not see a cause and assumed that it must be
something I had tweaked. At that time, I had forgotten that I had
added a function call to the constructor. I left it and simply
warned users that there was a harmless bug lurking in the
initialisation of a playpen window.</p>
<p>A couple of months ago I was running a quick test and happened
to write:</p>
<pre class="programlisting">
int main(){
  playpen paper;
}
</pre>
<p>In other words a bare minimum program using a playpen to which I
intended to add some more test code.</p>
<p>To my surprise, the computer seemed to lock up. After about a
minute it recovered and displayed an error message about a
misbehaving application.</p>
<p>This is no longer a matter that could be ignored. I experimented
a bit to discover what I had to do to avoid this lock-up. In the
end I discovered that doing almost anything that stopped the
program from closing immediately solved the lock-up problem.</p>
<p>I still did not understand the problem but I could see a simple
solution, insert a call to a wait function in the constructor of
playpen. [It was then that I realised that I had altered Gary's
code by adding the call to the function that provided my default
palette.]</p>
<p>Now, that fixed the other problem as well, the screen updated
correctly when a program created a playpen Window.</p>
<p>I reported my experience and fix to Gary. Now that gave him
enough of a clue to really fix the problem, which was in the
original code.</p>
<p>You may be wondering what this has to do with your code. My
playpen type relies on using multi-threading. Effectively it
implements all the special simple GUI functionality as a distinct
thread. The underlying problem was that programs were returning
from constructing a graphics window too quickly, before Windows had
finished.</p>
<p>This only shows up on modern fast hardware, where the CPU speed
is high enough to beat some other process.</p>
<p>This is a good example of the problems of multi-threading. You
cannot check your code is correct by testing (actually, you never
can do that). Your code may work perfectly for years and then start
to fail occasionally. You are certain that the code is OK because
it has worked for so long and assumes that the problem is somewhere
else. However increased hardware performance begins to make a real
problem manifest.</p>
<p>By hindsight, I can see how the symptoms should have pointed me
to the cause. The added function in the constructor was trying to
update the palette definition while the Playpen window as still
being constructed. However, had I tested a truly minimal program
much earlier I would have got a much better clue, because trying to
destroy a window before it has finished being created is sure to
provide a recognizable symptom.</p>
<p>Never ignore little irritants, and test code incrementally
starting with the simplest program you can write. Even a little
extra may hide a real problem.</p>
<p>I would be very happy to hear your comments on this and other
aspects of problems revealed by higher performance hardware.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e57" id="d0e57"></a>Commentary on
Problem 21</h2>
</div>
<p>Consider:</p>
<pre class="programlisting">
class x;
class xyz {
public:
  xyz();
  ~xyz();
  static int const elements(100);
// rest of interface
private:
  x * pointers[elements];
};

xyz::xyz(){
  for(int i(0); i != elements; ++i){
    pointers[i] = 0;
  }
}
  
xyz::~xyz(){
  for(int i(0); i != elements; ++i){
    delete pointers[i];
  }
}
</pre>
<p>Is there a better way to implement the constructor? Of course
there is, and I know some of you know it but do you?</p>
<p>Let us have a look at the code before dealing with the
problem.</p>
<p>The first point is that it looks as if the design intends that
xyz instances own the items pointed to by the elements of pointers.
If that is the case, why did I use an array of raw pointers? In
addition, why did I use a fixed size array? That seems to make very
little sense.</p>
<p>Without some idea as to what <tt class="classname">xyz</tt> is
supposed to do (and the name is not exactly helpful), it is hard to
see why I did not use something such as <tt class=
"literal">std::vector&lt;x&gt;</tt> or <tt class=
"literal">std::deque&lt;x&gt;</tt>. However consider the case where
x is not a copy constructable, nor copy assignable type. If it also
lacks a default constructor, we do not have many options left.</p>
<p>While testing the code I got a rude shock. First, this is what I
was looking for:</p>
<pre class="programlisting">
xyz::xyz(): pointers() {};
</pre>
<p>While we cannot explicitly initialise an array, we can force
default initialisation. In general, that gains us nothing, because
that would happen anyway. The default action for the fundamental
types is to do nothing; we can force zero initialisation with the
above syntax.</p>
<p>Now to the rude shock:</p>
<pre class="programlisting">
static int const elements(100);
</pre>
<p>is ill-formed. The compiler considers it an attempt to declare a
static member function and requires that an in class initialisation
be written as:</p>
<pre class="programlisting">
static int const elements = 100;
</pre></div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e95" id="d0e95"></a>Problem 22</h2>
</div>
<p>Well the problem is that I have run out of a ready supply of
little coding surprises and problems. It is time that you got
involved. Please send in at least one coding surprise. If you do
not have any then I guess you do not actually do much
programming.</p>
<p>The surprise can be in any of the programming languages that are
used regularly for application programming (C, C++, C#, Java,
Python etc.)</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e102" id="d0e102"></a>Cryptic clues
for numbers</h2>
</div>
<p>Neil Horlock sent in this clue for last issues number (471,
which is 20 more than 451 which was the title of a Ray Bradbury
novel about a post catastrophe world where books were burned as
evil.) The first part of my clue references the issue of C Vu in
which it appeared (17.4)</p>
<p>Roundabout a maximum break, a thousand less than the number of
the last caller.</p>
<p>I like his idea of using 'roundabout' to clue rotating the
digits, however I think the second part of the clue could do with a
bit more polish. Anyone like to propose improved wording based on
the same idea (for those from elsewhere in the World, 1471 is the
number for recovering the number of the last person who called
you.)</p>
<p>This issue's clue</p>
<p>One for love too? Sounds like the right day for it!</p>
<p>Provide your clue for the solution to my clue. No prizes, just a
warm fuzzy feeling of seeing your name in the next issue.</p>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
