    <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  :: Gotcha(); // describe some bugs</title>
        <link>https://members.accu.org/index.php/articles/1107</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">Programming Topics + CVu Journal Vol 13, #2 - Apr 2001</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/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c65/">Programming</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/c121/">132</a>
<br />

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

                    -                        <a href="https://members.accu.org/index.php/articles/c65+121/">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;Gotcha(); // describe some bugs</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 03 April 2001 13:15:44 +01:00 or Tue, 03 April 2001 13:15:44 +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>I am not a language lawyer, although I greatly admire people who
are. One thing I do seem to be good at is writing buggy code. I am
constantly amazed at how hard I find it to write correct code.
Sometimes the code does what I meant it to, but it's not correct
because I just didn't understand the problem domain well enough.
And sometimes the code just doesn't do what I meant it to. I
thought I would share some of the latter accidents in the hope that
someone would either learn something about C/C++, or derive some
amusement from my ignorance. Feel free to Laugh Out Loud.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e27" id="d0e27"></a>Delusion</h2>
</div>
<p>Recently I wrote code something like the following, and was
surprised when it didn't work:</p>
<pre class="programlisting">
void zeromac(unsigned char mac[6]){
  memset(mac, 0, sizeof(mac));
}
</pre>
<p>When I say it didn't work I mean it compiled without warnings
but when run it didn't do what I meant it to. What I wanted was to
write zero into each byte of the given 6-byte buffer. What it
actually did was to write only 4 zero bytes. It may be obvious to
you what is wrong with this, but when I wrote it, it seemed like a
reasonable thing to do as I was used to writing code such as the
following, which does work:</p>
<pre class="programlisting">
unsigned char mac[6];
memset(mac, 0, sizeof(mac));
</pre>
<p>Had I written the parameter as a pointer I would not have used
<tt class="literal">sizeof</tt>:</p>
<pre class="programlisting">
void zeromac2(unsigned char *mac){
  memset(mac, 0, 6);
}
</pre>
<p>In fact in the original function <span class="emphasis"><em>I
did declare the parameter as a pointer</em></span>, I had just
confused myself by using the array declarator syntax. So in the
original function when I wrote <tt class="literal">sizeof(mac)</tt>
I was getting the size of a pointer to a 6-byte array, and on my
machine the size of a pointer to something is 4. <span class=
"emphasis"><em>I consider myself lucky that the size of the object
I was trying to zero was different to the size of a
pointer</em></span>. Had they been the same the code would have
worked by accident and my delusion would remain.</p>
<p>You might think that the good thing about the original function
is that the compiler can check that the function is called with
objects of exactly the right size. (I did.) You might expect to get
a compile time error if you tried to do something like this:</p>
<pre class="programlisting">
unsigned char m[3];
zeromac(m);  // compile time error?
</pre>
<p>But you wouldd be disappointed. So long as you pass <tt class=
"function">zeromac()</tt> a pointer to an <span class=
"type">unsigned char</span> you won't get a compile time error. One
way to clarify the code and avoid the magic constant 6 in
<tt class="function">zeromac2()</tt> would be to use a <tt class=
"literal">typedef</tt>:</p>
<pre class="programlisting">
typedef unsigned char mac_t[6];
void zeromac3(mac_t mac){
  memset(mac, 0, sizeof(mac_t));
}
</pre>
<p>Here <tt class="literal">sizeof(mac_t)</tt> is 6, so that's OK
(<tt class="literal">sizeof(mac)</tt> is still 4). But you can
still pass any unsigned char pointer to <tt class=
"function">zeromac3()</tt>. One way you could get the compiler to
warn you of such mistakes is to use a reference:</p>
<pre class="programlisting">
void zeromac4(unsigned char (&amp;mac)[6]){
  memset(mac, 0, sizeof(mac));
}
</pre>
<p>or</p>
<pre class="programlisting">
void zeromac4(mac_t &amp;mac){
  memset(mac, 0, sizeof(mac));
}
</pre>
<p>In this case <i class="parameter"><tt>mac</tt></i> is a
reference to an array of 6 <span class="type">unsigned char</span>s
and <tt class="literal">sizeof(mac)</tt> is the size of the
referenced object, i.e. 6, which is correct. Also any attempts to
call <tt class="function">zeromac4()</tt> with other than an array
of 6 <span class="type">unsigned char</span>s will cause a
compile-time error, which is reassuring. In <tt class=
"function">zeromac4()</tt> because <i class=
"parameter"><tt>mac</tt></i> is a reference it will never be a null
pointer, which it might be in the first three functions.</p>
<p>I am sure there are many other ways to design a solution. One
obvious way would be to define a class to represent this data.
Say:</p>
<pre class="programlisting">
class Cmac{
public:
  void SetZero(){
    memset(m_data, 0, sizeof(m_data));
  }
  // other stuff
private:
  unsigned char m_data[6];
};
</pre>
<p>Then I could have safely written:</p>
<pre class="programlisting">
Cmac mac;
mac.SetZero();
</pre></div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e124" id="d0e124"></a>Led
Astray</h2>
</div>
<p>More than ten years ago I was got by a bug, which for some
reason has stuck in my mind. I don't remember the details of the
program but the bug occurred in a table of initialised data. Let us
say the table looked in part like this:</p>
<pre class="programlisting">
{&quot;cow&quot;, 2, 3, &quot;knife&quot;},
{&quot;cat&quot;, 192, 160, &quot;fork&quot;},
{&quot;dog&quot;, 43, 21, &quot;spoon&quot;},
</pre>
<p>I find tables of data where the columns don't line up very hard
to read, so I thought I would neaten the code like this:</p>
<pre class="programlisting">
{&quot;cow&quot;, 002, 003, &quot;knife&quot;},
{&quot;cat&quot;, 192, 160, &quot;fork&quot;},
{&quot;dog&quot;, 043, 021, &quot;spoon&quot;},
</pre>
<p>After all, leading zeros are ignored when reading a number.
Aren't they? Well the program didn't work in this neatened form and
I know now of course that &quot;A leading 0 (zero) on an integer
constant means octal; a leading 0x or 0X means hexadecimal.&quot;
[<a href="#KandR1988">KandR1988</a>] I didn't own a copy of K&amp;R
at the time (I seem to remember it cost something like &pound;40
then and I was too tight to buy myself a copy.) I was learning C by
using C. I don't think I would have mistakenly prefixed any numbers
with 0x, but zero alone seemed safe enough. Now it looks like a
stupid thing to do, why not use white space?</p>
<pre class="programlisting">
{&quot;cow&quot;,   2,   3, &quot;knife&quot;},
{&quot;cat&quot;, 192, 160, &quot;fork&quot;},
{&quot;dog&quot;,  43,  21, &quot;spoon&quot;},
</pre></div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e142" id="d0e142"></a>The
Interview</h2>
</div>
<p>This last gotcha is not really about a bug that got me, but it
is a story about my lack of knowledge of C++, and I did feel caught
out. A couple of years ago I was asked a question at a job
interview that I couldn't fully answer. The interviewer wrote three
lines on a board, something like this:</p>
<pre class="programlisting">
const char *p;
char const *p;
char * const p;
</pre>
<p>Then he asked me to describe what was being declared. I told him
that the first line declared that <tt class="varname">p</tt> was a
pointer to a constant <span class="type">char</span>, in other
words <tt class="varname">p</tt> could be modified but what
<tt class="varname">p</tt> pointed to could not. E.g. <tt class=
"literal">++p</tt> was permitted but <tt class=
"literal">++(*p)</tt> was not.</p>
<p>I told him that the last line declared that <tt class=
"varname">p</tt> was a constant pointer to a <span class=
"type">char</span>, in other words <tt class="varname">p</tt> could
not be modified but what <tt class="varname">p</tt> pointed to
could be. E.g. <tt class="literal">++p</tt> was not permitted but
<tt class="literal">++(*p)</tt> was.</p>
<p>But I didn't know what the middle line declared. I wasn't sure
whether the <tt class="literal">const</tt> referred to the
<span class="type">char</span> or the pointer or was incorrect
syntax or what. I have always found declarations one of the
trickiest parts of C++. (I wrote a short piece about declarations
which was printed in the July 2000 C Vu). At the time of the
interview I had been working with C and C++ for about ten years,
but I hadn't come across declarations in the form of the second
line before.</p>
<p>Actually the second line is valid and is semantically identical
to the first in both C and C++. In these first two declarations the
base type, <span class="type">char</span>, is qualified by
<tt class="literal">const</tt> and it doesn't matter whether the
qualifier comes before or after the type. In fact I think it is a
little more consistent to put the qualifier after the type as then
the second and third declarations may be read consistently right to
left: (2) <tt class="varname">p</tt> is a pointer to a constant
character, (3) <tt class="varname">p</tt> is a constant pointer to
a character. On the other hand (1) <tt class="varname">p</tt> is a
pointer to a character constant also makes some sense, so perhaps
it doesn't matter much.</p>
<p>My interviewer could have completed the set by asking me to
describe</p>
<pre class="programlisting">
char const * const p;
</pre>
<p>(<tt class="varname">p</tt> is a constant pointer to a constant
character), but he didn't. On the other hand it's interesting to
wonder why he asked me about these declarations at all; after all
he would have seen from my C V that I'd been writing C and C++ for
a decade. Did he think I was lying about my experience or did he
think I might not have come across declarations such as these in my
career? And yet, although I thought of myself as a moderately
experienced programmer, he had uncovered a fairly basic gap in my
understanding. On the other <span class=
"emphasis"><em>other</em></span> hand so what? Because I didn't
know a qualifier could be placed after a type I would not have
written code that way. And had I needed to work on someone else's
code containing declarations with qualifier after type I would have
learnt what this meant in pretty short order.</p>
<p>I didn't get the job (probably for some bigger gaffs than this).
<span class="emphasis"><em>C'est la vie</em></span>.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e231" id="d0e231"></a>Thanks</h2>
</div>
<p>I'd like to thank Kevlin Henney for reading a draft of this
article. Any errors that remain are my own.</p>
</div>
<div class="bibliography">
<div class="titlepage">
<h2><a name="d0e236" id="d0e236"></a>Reference</h2>
</div>
<div class="bibliomixed"><a name="KandR1988" id="KandR1988"></a>
<p class="bibliomixed">[KandR1988] Brian Kernighan &amp; Dennis
Ritchie The C Programming Language Second Edition, Prentice Hall,
1988.</p>
</div>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
