    <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 &amp; Answers</title>
        <link>https://members.accu.org/index.php/journals/777</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 11, #2 - Feb 1999 + Letters to the Editor</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/c133/">112</a>
                    (20)
<br />

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

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

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

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

                    -                        <a href="https://members.accu.org/index.php/journals/c133+186/">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 &amp; Answers</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 07 February 1999 13:15:29 +00:00 or Sun, 07 February 1999 13:15:29 +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>Questions from
Tony Houghton <tt class="email">&lt;<a href=
"mailto:tonyh@tonyh.tcp.co.uk">tonyh@tonyh.tcp.co.uk</a>&gt;</tt></h2>
</div>
<p>I read Harpist's &quot;Getting Started&quot; article, because even
programmers who consider themselves experienced can learn from that
sort of thing. I noticed what appears to be a bug. In the line
which first appears about a third of the way down the first column
on p.31 and is repeated nearer the bottom, surely:</p>
<pre class="programlisting">
++counters[tolower(data_string)];
</pre>
<p>should be:</p>
<pre class="programlisting">
++counters[tolower(data_string[iter])]; 
</pre>
<p>But what I'm really writing for is because I've got a query
about something he said in this article and another one about
something you said in a book review. Just below that problem bit of
code the Harpist said, &quot;I like that simple use of <tt class=
"function">strlen()</tt>....&quot; Does he mean he prefers:</p>
<pre class="programlisting">
for (iter = 0; iter &lt; strlen(data_string); ++iter)
</pre>
<p>to:</p>
<pre class="programlisting">
int length = strlen(data_string);
for (iter = 0; iter &lt; length; ++iter)
</pre>
<p>I thought the former was supposed to be anathema to anyone with
half a mind to efficiency.</p>
<p>He also mentions he prefers the pre-increment to post-increment.
Good.</p>
<p>I also tend to use pre-increment for the same reason
(post-increment can be less efficient for C++ classes) and wondered
if I was going against idiom because most C programmers seem to use
post-. so it's nice to know someone of the Harpist's calibre
considers I'm doing something right.</p>
<p>On to your review of &quot;Practical C Programming&quot;. On p.44 you
point out the line:</p>
<pre class="programlisting">
const char DATA_FILE[] = &quot;numbers.dat&quot;;
</pre>
<p>Your first comment is &quot;why a <tt class="type">const char</tt>,
rather than a <tt class="type">const char *</tt> or plain
<tt class="type">char</tt>?&quot; I can see the rest of what's wrong
with that line, but if I did want a hard-coded file name I'd tend
to write:</p>
<pre class="programlisting">
static const char data_file[] = &quot;numbers.dat&quot;;
</pre>
<p>(or perhaps use the name <tt class="literal">DataFile</tt> if I
wanted to use partial caps to indicate a non-macro constant). So
what's wrong with <tt class="type">const char[]</tt>?</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e75" id="d0e75"></a>Answers</h2>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e78" id="d0e78"></a>From the
Harpist.</h3>
</div>
<p>You are, of course, entirely correct in that I forgot the index
(and the second one was just cut and pasting from the first. Thanks
for spotting it.</p>
<p>In the second case, I agree that it might be better coding to
haul the calculation of the length out of the loop (I sort of
assume that compilers will do that for me &#61516;). My comment was
simply that I get tired of seeing students hand-coding things that
are already in the Standard C Library. I place great emphasis on
getting novices to use what is already available while reserving
their efforts to using it sensibly.</p>
<p class="c2"><span class="remark">The Harpist also commented on
pre-incrementing but I snipped it, see below.</span></p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e88" id="d0e88"></a>From Francis</h3>
</div>
<p>Let me expand a little on my brief comment quoted above.</p>
<p>There seems to me that there are two sensible alternatives.
Either for reasons best known to the code writer the program has
the file name hard coded into the executable or the file name is
being provided as a default value that maybe over-ridden at
execution time. Let me take the two choices separately.</p>
<p><span class="bold"><b>Choice 1</b></span>.</p>
<p>In this case you are providing a string literal and do not
intend it to be changed. So why provide it and then copy it into an
array that has been declared as non-modifiable? String literals are
semantically non-modifiable in C89 (in other words they behave as
arrays of <tt class="type">const char</tt>) and are actually
<tt class="type">const</tt> qualified in C++. If you want to
reference a string literal it seems to me to be much better style
to use a <tt class="type">const char *</tt>. This also allows the
value to be used in several translation units. In those where I do
not provide the definition I can write:</p>
<pre class="programlisting">
extern char const* data_file;
</pre>
<p>Note that I must not ever write:</p>
<pre class="programlisting">
extern char data_file[];
</pre>
<p>unless it was defined as an array. Declarations and definitions
of global variables must agree exactly, an array in one translation
unit and a pointer in another will not work.</p>
<p>Of course, in the context, I think any variation on this choice
is silly. However the point I was making is that having elected to
have a global <tt class="type">const</tt> variable it makes no
sense to turn it into an array with the extra potential for getting
it wrong as well as possibly getting an extra copy.</p>
<p><span class="bold"><b>Choice 2.</b></span></p>
<p>If the intention had been simply to provide a default file name,
I would have preferred one that resulted in a maximum size array
(for a file name).</p>
<p>I think that providing a default file name is not unreasonable,
and then you do have to provide an array of <tt class=
"type">char</tt> (but not <tt class="type">const</tt> qualified,
and have to be careful of the declarations and definition matching)
into which a user-selected alternative can be written at execution
time.</p>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e136" id="d0e136"></a>From Allan
Newton <tt class="email">&lt;<a href=
"mailto:amnewton@opticaldesign.co.uk">amnewton@opticaldesign.co.uk</a>&gt;</tt></h2>
</div>
<p><span class="bold"><b>Question 1</b></span></p>
<p>Reading C Vu 11.1 I was struck by the Harpist's comment (Getting
Started) to the effect that pre incrementing is better than post
incrementing in a loop counter i.e. this</p>
<pre class="programlisting">
for ( ctr = 0 ; ctr &lt; limit ; ++ctr )
</pre>
<p>better than</p>
<pre class="programlisting">
for ( ctr = 0 ; ctr &lt; limit ; ctr++ )
</pre>
<p>I have seen similar comments in other places but usually without
explanation or justification.</p>
<p>Could the Harpist or some other guru educate me please.</p>
<p>As the for loop is probably my most commonly used structure
statement and I tend to post increment I would like to know what I
am doing wrong and what benefits would accrue if I changed my
style.</p>
<p><span class="bold"><b>Question 2</b></span></p>
<p>Incidentally since I often loop over the same structure would it
be good or bad to adopt something on these lines:</p>
<pre class="programlisting">
#define LOOPSTART
    for ( unsigned long int ctr = 0 ; ctr != limit; ++ctr) {
#define LOOPEND
       }
</pre>
<p>Then I merely write everywhere</p>
<pre class="programlisting">
LOOPSTART
</pre>
<p>do something incredibly useful</p>
<pre class="programlisting">
LOOPEND
</pre>
<p>A relevant comment is that I find I make more typo's in what I
call &quot;housekeeping&quot; code like structure controls etc. than I do in
&quot;physics&quot; code. Interestingly my practice of trying to be
consistent with naming, layout and format is actually a hindrance
at tracking down the errors because of the well known proof readers
problem of reading what you expect to see. Having said that my
consistency approach surely reduces the number of errors by orders
of magnitude and will aid anyone else reading my code (a pretty
rare event)</p>
<p><span class="bold"><b>Question 3</b></span></p>
<p>As a total aside with the possibility of help and exchanges like
this why are not more of our members who work alone not fighting to
get onto the committee to develop the contacts that allow them to
discuss issues like this (albeit at a much more challenging
level).</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e180" id="d0e180"></a>Answers from
Francis</h2>
</div>
<p><span class="bold"><b>Answer 1</b></span></p>
<p>It is really a matter of developing habits that work in general.
In the days when the above code would necessarily have been C there
is no advantage one way or the other and programmers became
accustomed to using a post increment. The compiler almost certainly
generated absolutely identical code so we just wrote what seemed
most natural. Then along came C++ with the potential for user
defined post- and pre-increment. (The early versions of C++ did not
allow these to be distinguished but pretty soon mechanisms were
provided so that programmers could define both).</p>
<p>In the case of pre-increment there is no problem, the user
defined operator simply returns a reference to the object after
whatever you decide pre-increment means has been applied. The
problem comes with post-increment. How can we write a function with
a delayed action? The answer is that we cannot. What we have to do
is keep a copy of the un-incremented version, increment the
original and then return a copy of the copy. Even with a clever
compiler eliding one of those copies you still have to pay for at
least one copy.</p>
<p>The conclusion is that if you have a choice, use pre-increment.
With the increasing use of general iterators (pointers are just a
special case) we should get in the habit of writing our code so
that it works well with any iterator.</p>
<p>There is a second change that occurs because of the increasing
use of general iterators and that is how we code the test for
repeating the loop. The original idiom was to write <tt class=
"literal">iter&lt;limit</tt> on the basis that this avoided the
problem of jumping over the <tt class="literal">limit</tt>.
Actually, with good coding discipline (never change a loop variable
inside a loop) that does not happen. However with the growing use
of iterators that test is no longer likely to work in all normal
cases. There is no reason why an iterator for a <tt class=
"classname">deque</tt> (double ended queue) or a <tt class=
"classname">list</tt> should work appropriately. What we have to do
is to check that we haven't reached the end. Hence the test is now
more often coded as <tt class="literal">iter!=limit</tt>.</p>
<p>I think that understanding idioms is important so you will know
when to ignore them, but it is also important to use the idioms
appropriate to modern coding techniques.</p>
<p><span class="bold"><b>Answer 2</b></span></p>
<p>It isn't my style, but in C++ where the scope of a loop variable
is the life of the loop the idea certainly has some merit. However
I would suggest that something like:</p>
<p>#define LOOPSTART (limit) for ( unsigned long int ctr = 0 ; ctr
!= limit; ++ctr) {</p>
<p>would work better as a generalisation.</p>
<p><span class="bold"><b>Answer 3</b></span></p>
<p>Of course you do not need to be a Committee member to get help.
However it is often easier to discuss issues with those you know
rather than with faceless gurus. All the more reason for taking
every opportunity to establish contact with as many others as you
can. Being a Committee member is only one of several ways of
meeting some experts. Conferences, Seminars and Standard Meetings
are just three others that spring to mind.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e225" id="d0e225"></a>Idris S Hamid
Asked</h2>
</div>
<p>My name is Idris S Hamid. I have a MA in Physics and a PhD in
Philosophy, and am now an assistant professor. I am just starting
on the road to learning C++ (my only other substantial programming
experience is in TeX/LaTeX). I would like to learn C++, Java, and
Qt (the cross-platform C++ GUI library of KDE fame) for developing
TeX/LaTeX-related applications to use in my own research (W95,
presently switching to Linux). I am thus interested in joining
ACCU. Which of the four types of subscription would you recommend,
and what is the cost in US dollars? Does the C++ Specialist Group
cover Qt at all? Where do I send payment?</p>
<p>A question (if its ok with you): After carefully looking through
your book reviews, it appears that the best two books to start me
off on my journey may be &quot;C++ Interactive Course'&quot; by R Lafore (it
got some of the best reader reviews on amazon.com; both they and
your reviewer were iffy about the internet component) and &quot;C++ From
the Beginning'&quot; by Jan Skansholm (which got the highest praise from
your reviewer but no reader comments on amazon.com). Would you
recommend either of these, both, or something else for someone in
my position?</p>
<p>If you have any advice on how a novice like myself can get the
most out membership in your association, then feel free to pass on
any words of advice. <span class=
"emphasis"><em>Idris</em></span></p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e236" id="d0e236"></a>Answers</h2>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e239" id="d0e239"></a>from Reg Charney
<tt class="email">&lt;<a href=
"mailto:charney@CharneyDay.com">charney@CharneyDay.com</a>&gt;</tt></h3>
</div>
<p>Thank you for writing and asking to join the ACCU here in the
U.S. WRT price and membership issues, I will refer you to the
enclosed membership application form.</p>
<p>WRT my book recommendations, I would recommend the ones you
mentioned. I would also mention Bruce Eckel's &quot;Thinking in C++&quot;.
His style is nice and easy and he is quite accurate. The book
itself is available over the net.</p>
<p>See his site at <a href="http://www.EckelObjects.com/" target=
"_top">http://www.EckelObjects.com/</a> .</p>
<p>Getting the most out of the ACCU involves getting involved.
Currently, you can contribute questions and articles to our
magazines Overload or C Vu. In the near future, we plan to have
email reflectors, one will be specifically designed for newcomers
and students.</p>
<p>If you have any other questions, please feel free to contact me
or others in the ACCU. Again, thanks. <span class=
"emphasis"><em>Reg</em></span>.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e260" id="d0e260"></a>Francis'
Addendum</h3>
</div>
<p>Setting people like Idris on the right track is important
because others will seek and follow his advice. One major problem
is that people tend to recommend what worked for them. In the fast
changing world of C++ this will often result in poor advice. If you
learnt C++ more than ten years ago you were likely to have been a C
programmer already and you might well have learnt C++ from 'The C++
Programming Language' (1st edition). You might think that the best
way to learn C++ was to learn C first, then study the same book
(perhaps in its latest edition). If you find that silly, I can
assure you that such recommendations are far from unusual.</p>
<p>Over the last few years two things have been happening: C++ was
being developed and extended and our understanding of how to use
C++ has been maturing. Whatever my current first choice would be
for someone such as Idris (I think Eric Nagler's Learning C++ - A
Hands-On Approach 2nd edition is a good contender) you can be
pretty certain that it would be different by this time next year. I
know of at least one excellent contender currently being
written.</p>
<p>Let me offer a few criteria for a modern book on C++ for
newcomers:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>It does not assume familiarity with C</p>
</li>
<li>
<p>It does not insist on first teaching you C mechanisms (for
strings, arrays etc.)</p>
</li>
<li>
<p>The example code will run on current compilers (therefore
namespace &#61555;&#61556;&#61540; must be handled somehow)</p>
</li>
<li>
<p>It uses the STL in simple ways.</p>
</li>
</ul>
</div>
<p>Please note that we have just added a mailing list <tt class=
"email">&lt;<a href=
"mailto:accu-prog-questions@accu.org">accu-prog-questions@accu.org</a>&gt;</tt>.
This is a moderated list to ensure that inappropriate questions
(please do my homework) are not posted and likewise for answers
(helping with homework generated questions OK, doing the work is
not). I hope that a good number of the ACCU 'experts' will lurk on
this list so they can field questions in their areas of
expertise.</p>
<p>To a large extent, the more you put into ACCU, the more you get
out. There are a few exceptions among our most expert members.</p>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e289" id="d0e289"></a>Questions
from Anthony Pleace <tt class="email">&lt;<a href=
"mailto:apleace@apleace.demon.co.uk">apleace@apleace.demon.co.uk</a>&gt;</tt></h2>
</div>
<p>Dear Francis</p>
<p>I am a teacher in a Design and Technology Dept of a secondary
school. I have Borland turbo C++ ver 3 for DOS and am a member of
ACCU. I am not good at programming. I have a pupil who has built a
device which measures the length of pulses produced by a lighting
unit in order to understand how the lighting unit works.</p>
<p>I have built a parallel to serial interface for the pupil so
that the information can be captured by a computer and inspected
for any patterns.</p>
<p>My device will produce RS232 data in a constant stream at 9600.
Can you help me to sort out how to get this raw data which might be
any value including 0x00 and 0xff into the computer and then into a
file. I have found a program called TXNUL on C Vu disc 10.1 but I
cannot find any associated articles.</p>
<p>You could help by</p>
<div class="orderedlist">
<ol type="1">
<li>
<p>How do I get back issues of C Vu I think that I want Sept 97 to
go with TXNUL</p>
</li>
<li>
<p>Can all binary values be successfully transmitted to the serial
port</p>
</li>
<li>
<p>How do I store the information in a file in real time? Do I
collect the data in an enormous array (1K bytes??) and then put it
into a file or will file storage work successfully at this speed
9600?</p>
</li>
<li>
<p>If you are unable to help can you point me in another
direction</p>
</li>
</ol>
</div>
<p class="c2"><span class="remark">Not being an expert in this
field I forwarded his request to a couple of ACCU's experts in that
field. Francis</span></p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e320" id="d0e320"></a>Answers from
Brian Bramer <tt class="email">&lt;<a href=
"mailto:bb@dmu.ac.uk">bb@dmu.ac.uk</a>&gt;</tt></h2>
</div>
<p>I looked in past C Vus for article on TXNUL and cannot find
anything. (I do not think there were any Francis)</p>
<p>However I attach some files (these will be on this issue's
disk)</p>
<div class="variablelist">
<dl>
<dt><span class="term"><tt class=
"filename">rs_lib.h</tt></span></dt>
<dd>
<p>header file with prototypes of functions for serial line</p>
</dd>
<dt><span class="term"><tt class="filename">library
rs_lib.c</tt></span></dt>
<dd>
<p>implementation file of serial line library</p>
</dd>
<dt><span class="term"><tt class=
"filename">terminal.c</tt></span></dt>
<dd>
<p>test program uses the above as a simple terminal emulator</p>
</dd>
</dl>
</div>
<p>You compile terminal.c so:</p>
<pre class="screen">
  tcc terminal.c rs_lib.c
</pre>
<p>and assuming your experiment is attached to serial port COM2 at
9600 baud run the terminal emulator so</p>
<pre class="screen">
  terminal -port=com2 -baud=9600
</pre>
<p>You should see any printable characters from your experiment.
You can then write a program to acquire your data and process it as
you wish.</p>
<p><tt class="filename">rs_lib.c</tt> uses interrupts to read
characters and stores them in a ring buffer rs_data of length
rs_length which is currently 1024 bytes - you can increase this if
necessary, i.e. if you get overrun errors. You call the <tt class=
"function">rs_getch()</tt> functions to read the characters from
the ring buffer.</p>
<p>This will read null characters and 0xff without problem under
DOS and so long as your have a reasonably fast machine it should
work OK at 9600 baud.</p>
<p>You may need to run you PC in DOS mode.</p>
<p>Let me know if you have any problems</p>
<p class="c2"><span class="remark">While this fixed the direct
problem, I think it highlights an area where a couple of simple
articles might be welcome. I know you can go and buy a book on the
subject, but not everyone wants to wade through such material
trying to decide whether the author is an expert at electronics or
at programming (very few are both).</span></p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e377" id="d0e377"></a>Question from
Jeff Spence <tt class="email">&lt;<a href=
"mailto:JSpence647@aol.com">JSpence647@aol.com</a>&gt;</tt></h2>
</div>
<p>I am a novice-hobbyist and a fairly new member of the ACCU. I am
currently learning C++ via C++ Primer (Lippman &amp; Lajoie). The
book was recommended by the ACCU and I am very satisfied with it.
However, it seems to me that this book, as well as some other books
I purchased prior to my membership, do not discuss the creation and
management of data files in great detail. Should I be satisfied
with what is offered in &quot;C++ Primer&quot; and eventually develop the
necessary skills on my own, or should I seek another source to
compliment my learning? Also, are there other areas that I should
be concerned with so as to learn all that a good programmer needs
to learn? Thank you for your time.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e384" id="d0e384"></a>Answers?</h2>
</div>
<p>Over to you, dear reader.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e389" id="d0e389"></a>A Quick
Question from Richard Howells <tt class="email">&lt;<a href=
"mailto:R_A_Howells@compuserve.com">R_A_Howells@compuserve.com</a>&gt;</tt></h2>
</div>
<p>On a recent C++ class a student asked me about resources for
setting C++ programming standards.</p>
<p>I'd be interested if you could suggest any good books, or other
resources.</p>
<p>I checked the web site and I did not find anything obvious
there.</p>
<p class="c2"><span class="remark">I think this question is about
C++ programming guidelines. The best book on the subject is
<i class="citetitle">Industrial Strength C++</i> by Henricson &amp;
Nyquist. <i class="citetitle">Large Scale C++ Software Design</i>
by Lakos should also be on their reading list.</span></p>
<p class="c2"><span class="remark">What do readers have to add?
Particularly with regards to other resources. Francis</span></p>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
