    <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  :: Questions and Answers</title>
        <link>https://members.accu.org/index.php/journals/985</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>


        <h2>Journal Articles</h2>


<div class="xar-mod-head"><span class="xar-mod-title">CVu Journal Vol 12, #2 - Mar 2000 + Programming Topics</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/journals/">All</a>

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

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

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c127/">122</a>
                    (18)
<br />

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

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c65/">Programming</a>
                    (877)
<br />

                                            <a href="https://members.accu.org/index.php/journals/c127-65/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/journals/c127+65/">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;Questions and Answers</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 05 March 2000 13:15:35 +00:00 or Sun, 05 March 2000 13:15:35 +00:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e20" id="d0e20"></a></h2>
</div>
<p>First a letter about something I wrote about the undefined
behaviour in <tt class="literal">printf(&quot;%d %d&quot;, fn(i++),
fn(i++))</tt></p>
<p>Hi Francis,</p>
<p>Actually you missed one. A few years ago when I was &quot;doing&quot; the
&quot;when does the <tt class="methodname">++</tt> actually happen&quot;
thing, I was experimenting with Borland C++ v3 or 4, can't remember
which, and I was using the expression <tt class="literal">i++ + i++
+ i++ + i++ + i++</tt>, with <tt class="varname">i</tt> initialised
to 1 this produced an output of 5 and <tt class="varname">i</tt>
was left at 10. Drilling down to the machine code level I found
that the <tt class="methodname">++</tt>es were being left until
&quot;after&quot; the ';', so effectively I had <tt class=
"literal">printf(&quot;%d&quot;,i+i+i+i+i); ++ ++ ++ ++ ++</tt>. If this is
still true for Borland 5, you should find that <tt class=
"literal">printf(&quot;%d %d&quot;, fn(i++), fn(i++))</tt> with <tt class=
"varname">i</tt> initialised to zero will call <tt class=
"function">fn</tt> with the value zero both times resulting in the
output '1 1'. THEN, <tt class="varname">i</tt> will be <tt class=
"methodname">++</tt>'ed twice, and will be left with the value
2.</p>
<p>Dave Spence <tt class="email">&lt;<a href=
"mailto:dwspence@uk.oracle.com">dwspence@uk.oracle.com</a>&gt;</tt></p>
<p class="c2"><span class="remark">I do not think I missed anything
other than failing to present one more way in which the user can
get unexpected results. Borland are entitled to defend themselves
on the basis that the code has undefined behaviour. The existence
of sequence points at entry and exit to a function is irrelevant
because the evaluation of i++ in each case is external to the
function. The function needs the value but does not have access to
the storage. It might be slightly different in the case of C++
references. Francis</span></p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e70" id="d0e70"></a>Answers</h2>
</div>
<p>Now to a few answers</p>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e75" id="d0e75"></a>Trap - Answer from
Nigel Eke <tt class="email">&lt;<a href=
"mailto:nigel.eke@bcs.org.uk">nigel.eke@bcs.org.uk</a>&gt;</tt></h3>
</div>
<p>In the last issue I published a short problem in my Bits &amp;
Pieces column (as a filler) which could just as well have gone into
the Q&amp;A column. Being a little short of responses for this
column I have reclassified the following response.</p>
<p>The possible trap that can be fallen into with the <tt class=
"classname">Fused</tt> class becomes clear if the lifetime of a
<tt class="classname">Fused</tt> object and the lifetime of the
referenced integer are considered. If the lifetime of the object is
longer than the lifetime of the referenced integer then you just
need to ask yourself the question &quot;What is being referred to if the
object uses the member variable <tt class="varname">icr</tt> after
the original referenced integer no longer exists?&quot;</p>
<p>Falling into the trap-for the purposes of this demo I made
<tt class="varname">icr</tt> <tt class="literal">public</tt>:</p>
<pre class="programlisting">
#include &lt;iostream&gt;
class Fused {
public:
  int const&amp; icr;
  Fused (int const&amp; iref) : icr(iref) {}
};
Fused* foo (){
  int const anInteger = 4;
  return new Fused (anInteger);
}
void bar (){
  char a[] = &quot;Mary had a little lamb&quot;;
  cout &lt;&lt; a;
}
int main(int argc, char **argv){
  Fused* pFused (foo ());
// &quot;anInteger&quot; no longer exists,
// so neither does &quot;pFused-&gt;icr&quot;
  bar (); 
// Use the stack area where icr&amp; is located...
  cout &lt;&lt; pFused-&gt;icr &lt;&lt; endl;
  delete pFused;
  return 0;
}
</pre>
<p>I think the above will probably demonstrate the problem in most
environments - it certainly does with Windows NT3 / Borland C++
Builder 3.</p>
<p class="c2"><span class="remark">Editor: Nigel has spotted the
essence of the problem. The vicious twist is that because of the
use of a const &amp; it will even bind the class reference to a
temporary so the following declaration will, I think,
compile:</span></p>
<pre class="programlisting">
int main(){
  Fused fuse(4);
  cout &lt;&lt; fuse.icr;
}
</pre></div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e110" id="d0e110"></a>Answer to
question 1 from Fred Johnson <tt class="email">&lt;<a href=
"mailto:fred@fredshome.freeserve.co.uk">fred@fredshome.freeserve.co.uk</a>&gt;</tt></h3>
</div>
<div class="sect3" lang="en">
<div class="titlepage">
<h3><a name="d0e115" id="d0e115"></a>Problem with odd
length data packets.</h4>
</div>
<p>I am afraid the answer to the above problem is not as simple as
it would first appear. Firstly, the reasons for byte orientated
software. This is due to two factors.</p>
<div class="orderedlist">
<ol type="a">
<li>
<p>Early microcomputers were built around 8-bit systems and it was
convenient for the manufacturers to produce an 8-bit uart for
conversion from parallel to serial mode.</p>
</li>
<li>
<p>Encoding systems for data (e.g. ASCII) commonly used 7 or 8 bits
to represent a character in serial transmission. Since these
systems are now regarded as 'standard', any move to change them
without overwhelming need will be almost impossible to achieve.</p>
</li>
</ol>
</div>
<p>There are two ways in which your requirements may be
satisfied</p>
<div class="orderedlist">
<ol type="1">
<li>
<p>Build your own (very non-standard) uart and serial card. A very
expensive and time consuming, non-trivial process.</p>
</li>
<li>
<p>Make use of current 8-bit technology and send the data in two
bytes.</p>
</li>
</ol>
</div>
<p>At this point, you can either pad out the bit count to 16 with
zero's or alternatively, if your application is safety critical,
use the remaining bits in an error correction system.</p>
<p>As for references, the only one I have seen is one chapter of
the book The C Toolbox by William James Hunt, published in 1989 by
Addison-Wesley, This is now very old and outdated, and also
confines itself to IBM PC systems only. You do not say what
platform you are working on.</p>
<p>The author does quote another source, <i class="citetitle">C
Programmers Guide to Serial Communications</i> by Joe Campbell, but
I have not personally seen this book.</p>
</div>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e145" id="d0e145"></a>Answer to
Question 4 (converting to all uppercase )</h3>
</div>
<div class="sect3" lang="en">
<div class="titlepage">
<h3><a name="d0e148" id="d0e148"></a>from R.L.
Thornburrow <tt class="email">&lt;<a href=
"mailto:C9650294@hud.ac.uk">C9650294@hud.ac.uk</a>&gt;</tt></h4>
</div>
<p>Converting a string to uppercase is simply a matter of iterating
through the string and copying it character by character changing
all lowercase letters to uppercase. The best way of achieving this
is by using the function <tt class="function">toupper()</tt> that
forms part of the standard C library (which is also accessible
through C++). Using <tt class="function">toupper</tt> (as with
<tt class="function">tolower</tt> which performs the reverse
action) is very useful as the case of any lowercase letters
including any accented or locale specific characters are changed.
Characters already in the desired case, symbols, numbers and the
like remain the same. <tt class="function">toupper</tt> can also
determine which character code system is being used (for example
ASCII code 65 is A) enabling it to work with other code systems.
This increases the portability of the code as system specific
issues such as this are resolved by the libraries.</p>
<p>The code below will solve the problem.</p>
<pre class="programlisting">
string upper_case(string const &amp; s) {
  string::const_iterator p = s.begin();
  string s1 = &quot;&quot;;
  while (p != s.end()) {
    s1 += toupper(*p);
    ++p;
  }
  return s1;
}
</pre>
<p>As the original string is being passed as a const to prevent it
from being modfied by the function, the string iterator also has to
be declared as a const, hence the use of const_iterator.</p>
</div>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e174" id="d0e174"></a>New
Questions</h2>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e177" id="d0e177"></a>Question 1:
Clipping Trailing Zeros</h3>
</div>
<p>I am trying to output a <tt class="type">double</tt> to a string
using <tt class="function">sprintf</tt>. The code I am using right
now is</p>
<pre class="programlisting">
sprintf(buf, &quot;%f &quot;,d); 
</pre>
<p>where <tt class="varname">d</tt> is a value of type <tt class=
"type">double</tt>. What this does is automatically output to six
decimal places. So if d = 1.0 buf becomes '1.000000.' A more
relevant example would be <tt class="literal">d = 3.142</tt>
resulting in generating '3.142000' in buf Is it possible to clip
all the trailing zeroes from the output of a <tt class=
"type">double</tt>?</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e204" id="d0e204"></a>Question 2:
Pointer problem</h3>
</div>
<p>Why doesn't the following change the pointer it is passed?</p>
<pre class="programlisting">
void findspace(int * points_at, char * text){
  int i;
  for(i=0; i&lt;strlen(text); i++)
    if(text[i] = ' ') points_at = text+i;
  return
}
</pre></div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e211" id="d0e211"></a>Question 3: What
id Koenig Lookup and what use is it</h3>
</div>
<p>Can someone explain what on Earth Koenig lookup is. I used to
think I understood how overloading worked in C++ but this seems to
be no longer true.</p>
</div>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
