Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Letters


JUL93: LETTERS

LETTERS

Sometimes the Best Defense Isn't a Good Offense

Dear DDJ,

Defensive programming is something many of us have consciously or unconsciously adopted over the years, usually from bitter experience of the kinds of things that can happen without it. Simple things like using possibly redundant parentheses to ensure that you get the precedence you want from a sequence of C operators, for example, or automatically using separate source files as a means of data hiding unless there are overwhelming reasons not to.

OOP was originally touted as eliminating or greatly reducing the need for such strategies, and after a couple of years' C++, use I would agree that this is generally true. However, beware! The new languages and techniques can themselves call for new defensive ploys.

For instance, take the increasingly common style being used for C++ class declarations. Everywhere I look--in textbooks, compiler manuals, help files, source code for professional class libraries, and, yes, even DDJ listings--I see code along the lines of Example 1. This is perfectly legal C++ and will give you exactly the data protection you expect--as long as you get the code right. But sooner or later, Murphy's law will assert itself and that "private" statement will get left out. The code will still be legal; it will compile okay and your test programs will very likely run perfectly. The data protection is gone, however. Worse, if your formal documentation is done by someone else on the team or by some smart new documentation software, the ultimate users of your brilliant class library will be under the impression that it's perfectly okay to tinker with the very class members you wanted to hide from them. This is a surefire recipe for Interesting Times.

The defensive programming strategy against Murphy and his law is simple--always put the "private" section first. If you leave out the "private" statement, the section members will stay private by default. And if Murphy trashes your "public" statement, you will find out all about it the minute you try to compile code testing the class "public" interface.

Robert Sproat

London, England

LUC Redux

Dear DDJ,

My May 1993 letter to DDJ regarding Peter Smith's article, "LUC Public-key Encryption" (January 1993) had an important misprint (perhaps because of a poor fax transmission). The correct value for r should read: r=lcm(p1e1-1(p12-1),...p1e-1(p12-1)).

Willi More

Klagenfurt, Austria

mapdev()for Fortran

Dear DDJ,

Ken Hamilton forgot one Fortran compiler in his article, "Direct Memory Access from PC Fortrans" (DDJ, May 1993)--Microway's NDP Fortran. Not only was NDP Fortran the first 32-bit Fortran for protected-mode DOS, but it was also the first to employ a concept called "map device," which allows Fortran programmers to map physical or virtual memory and/or devices into their Fortran applications through the seamless interface. These mappings include video, BIOS, dongles, disk drives, the Weitek coprocessor, and so on, all of which can be driven by NDP Fortran.

I've included a program that demonstrates the expressive power behind Microway's mapdev() function. See Example 2 for details. This program accomplishes what the other protected-mode Fortrans mentioned in Ken's article cannot.

Mark J. Barrenechea

Microway

Kingston, Massachusetts

Fortran Fan

Dear DDJ,

I wanted to say how pleased I was to see an article employing the Fortran language in the May issue of Dr. Dobb's Journal. I very much enjoyed Kenneth Hamilton's article on "Direct Memory Access from PC Fortrans" and will be able to put the material to immediate use. Please consider giving greater prominence in the future to numerical scientific applications and the Fortran language in which they are commonly programmed.

Michael L. Berbaum

Research Social Scientist

Tuscaloosa, Alabama

DDJ responds: Glad you enjoyed the Fortran coverage, Michael. Watch for our September 1993 issue which will examine numerics and numerical programming.

C-like Assembler for DSP--Not!

Dear DDJ,

I recently learned that I will be writing code for an AT&T DSP32C, so I was especially interested in Mac Cody's article, "A Wavelet Analyzer" (DDJ, April 1993), in which the implementation is based on that processor. This was the first programmer-oriented information I had found about any of AT&T's DSPs since starting an informal search involving the Internet and trips to several technical bookstores. Are there any logical reasons for this surprising lack of publicly available information?

When I looked at Mac's assembly-language source code and subsequently read that one of the reasons that he chose AT&T's DSP32 and DSP32C was because of their "C-like assembly languages," I almost screamed. I strongly feel that this is a great liability rather than a bonus. I've done a great deal of assembly-language programming (mainly in the field of real-time computer graphics), and there's a vastly different mindset needed to write "good" assembly language than that required to write "good" C. The different look of most assembly languages helps you get into that mindset. I greatly prefer the look-and-feel of Motorola 680x0 syntax (my favorite assembly language) to that of AT&T DSP32 and DSP32C.

I also feel that "normal" assembly language is much more straightforward than this "C-like" assembly language. Ask youself, for example, which instruction in Example 3 is easier to understand and deal with: the slightly modified Motorola 5600xMAC (Signed Multiply-Accumulate) or the example from the DECOMP routine?

Both instructions do the same thing, but with the former you need to remember more rules about the construction of such expressions. The latter just requires you to chose the correct instruction for the risk. Using the same symbol for more than one thing can also add more chances for errors to occur (for example, + for addition vs. ++ for postincrement and * for multiply vs. * for indirect referencing).

Occasionally, I accidentally do something like leave off an operand. The assembler tells me right away what's wrong and I feel a bit stupid while I fix it. With C-like syntax, an instruction with a missing operand could very easily turn out to be a valid different instruction. Bugs like that are much harder to find_. As an added bonus, the latter takes less time to type.

Maybe my intense dislike for the AT&T DSP32 and DSP32C assembly languages is rooted in the fact that I believe in highly optimized (speed and size) code, even on fast processors, and am the type of person who says things like, "Gee, that's an interesting instruction_. Now how can I use it to my advantage?"

Jesse Michael

Portland, Oregon

So How Was Your Date?

Dear DDJ,

I greatly enjoyed Peter Meyer's "Julian and Gregorian Calendars" article (DDJ, May 1993). With many systems still relying on date formats that have only two digits for the year, there will be much more interest in date routines as the year 2000 approaches.

His brief mention of Easter in the article brought to mind a ten-step algorithm that, according to one source I've seen, is over 100 years old. This routine is said to incorporate the paschal full- moon determinations and seems to work for "modern" years; see Table 1, where Easter is the nth month and the (P+1)st day.

Karl Hoppe

Orange, California

More Genetic Algorithms

Dear DDJ,

I was very excited to see the topic of your February '93 issue, "Cognitive Computing." I enjoyed all four articles. Despite this, I was disappointed to see that your list of software included so little of the available genetic-algorithm software. My company produces a C++ package for the Macintosh and Windows called MicroGA. There are also a number of public-domain packages available. The easiest way to locate these is probably through the Internet discussion group of GAs. If you wish to join this group, send your name and e-mail address to GA-List-Request@

AIC.NRL.NAVY.MIL. I hope the staff at DDJ keeps up the good work, and keeps covering leading-edge technologies such as these.

Stephen D. Wilson

Emergent Behavior

Palo Alto, California

Example 1

class SomeClass();
public:
int this;
char that;
void TheOther();
 ...
private
 long vulnerable;
 float risky;
 int Disastrous();

Example 2

c  This program uses mapdev to map the video buffer into
c  data space and poke values into which  will be displayed.

c  grex.fh contains declarations for GREX (GRaphic EXtensions)
c  function get_bios_mode. os.fh contains declarations for mapdev,
c  peekb, and peekw
    include 'grex.fh'
    include 'os.fh'

  integer screen,addr,count
  integer bios_mode,lines,cols

  addr = z'b8000'
  if (get_bios_mode()eq 7 addr = z'b0000'

  screen = mapdev (addr,8192)
    write(*,*) 'scren pointer = ',screen
c   Get size of video page
  lines = 1 + peekb (z'484')
  cols = peekw (z'44A')
    call pauseb
c   write values to video buffer, to be displayed on screen
  call try_it (% VAL(screen),2*cols,lines)
  call pauseb
end
  subroutine try_it (a,m,n)
  integer m,n
  character a(m,n)
  do j = 1,n
k = ()
do i=1,m,2#
   k = k+1
   a(i,j) = char(k)
   a(i+1,j) = char(j)
enddo
enddo
  return
  end

Example 3

aO = aO + *r3++ **r1++;   /* AT&T DSP32 and DSP32C syntax */

mac (r3)+,(r1)+,aO        ; Modified Motorola 6500x syntax


Table 1

===========================================================================
   Step                          Result                   Values for 1993
                           Quotient  Remainder
===========================================================================
   Year/19                    --        A                   --       17
   Year/100                    B        C                   19       93
   B/4                         D        E                    4        3
   (B+8)/25                    F       --                    1
   (B-F+1)/3                   G       --                    6
   (19A+B-D-G+15)/30          --        H                   --       17
   C/4                        --        K                   23        1
   (2E-H+2I-K+32)/7           --        L                   --        3
   (A+11H+22L)/451             M       --                    0
   (H+L-7M+114)/31             N        P                    4       10
===========================================================================


Copyright © 1993, Dr. Dobb's Journal


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.