home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / s / snip1292.zip / C_PORT.TXT < prev    next >
Text File  |  1991-08-04  |  13KB  |  341 lines

  1.  
  2. =====[ Ed Hopper's BBS #1 ]=====[  8-04-91 ]=====[  9:55.22 ]=====
  3.  
  4.  
  5. Date: 08-02-91 (09:40)       C-Lang Number: 26406 (Echo)
  6.   To: ALL                      
  7. From: JOSEPH CARNAGE                  Read: 08-03-91 (10:56)
  8. City: DUNEDIN FL                   Last On: 02-28-91 (22:53)
  9. Subj: Portable, clean code #1  
  10.  
  11.                How to write portable clean source code
  12.                ---------------------------------------
  13.  
  14. A common concern with programmers new to Axiom is writing portable
  15. code.  There a number of tricks and guide lines which may help with
  16. this.  In no particular order:
  17.  
  18.   --  Use full ANSI prototypes with all arguments declared.  For
  19.       function pointers, declare their expected arguments.  For
  20.       prototypes for functions which accept function pointers, do not
  21.       declare the expected arguments for the function pointer
  22.       argument.
  23.  
  24.       It is good practice to put dummy variable names in prototypes as
  25.       this adds readability.
  26.  
  27.   --  Explicitly type all variables and functions.  Never rely on them
  28.       defaulting to int.
  29.  
  30.   --  Pay a little care to the ternary operator ? :, and parenthesize
  31.       heavily.  A very few compilers have problems with the default
  32.       order of evaluation for the ternary operator.
  33.  
  34.   --  Never ever name a variable identically to a function.  This is
  35.       most especially true of statics or globals.  This sort of error
  36.       can cause weird hidden linker problems which cause bizarre
  37.       results at runtime and are difficult to trace.
  38.  
  39.   --  Never ever name a screen, form, data field etc identically to a
  40.       variable or function as this can cause weird and non-reported
  41.       linker errors in the final executable which can be very
  42.       difficult to locate.
  43.  
  44.   --  Do not nest comments.  If you want to block off a section of
  45.       code temporarily, use #ifdef/#endif.  eg.
  46.  
  47.            ...code...
  48.          #ifdef JUNK /* Unwanted code */
  49.            ...more code...
  50.          #endif /* JUNK */
  51.            ...yet more code...
  52.  
  53.   --  Run PC-Lint on all code, and handle ALL errors and warnings.
  54.  
  55.   --  Read the "Frequently Asked Questions" document and understand
  56.       fully.
  57.  
  58.   --  Read K&R thoroughly and everywhere it mentions "implementation
  59.       dependant", or "new" features not "supported by all compilers",
  60.       avoid those areas.  Examples are bit fields, passing structures
  61.       to functions, returning structures from functions, and the
  62.       volatile type.  These are not supported by all compilers.
  63.  
  64.   --  In areas where the ANSI standard advances on the old K&R, but
  65.       still allows the K&R form, follow K&R.  An example is using
  66.       function pointers.  If you are unsure what areas this covers,
  67.       don't worry, just stick with K&R.  eg.
  68.  
  69.       Given:
  70.  
  71.         int (*prj_afunc) (); /* Pointer to function which returns int */
  72.  
  73.       ANSI allows the pointed to function be called as so:
  74.  
  75.         prj_afunc (xxx, yyy);
  76.  
  77.       K&R specifies that it should be called:
  78.  
  79.         *prj-afunc (xxx, yyy);
  80.  
  81.       Use the K&R form, which ANSI still allows, and all compilers
  82.       support.
  83.  
  84.   --  Do not use the new // comments.  Stay with the /* comment */
  85.       form.
  86.  
  87.   --  Do not indent #ifdefs, #defines, #pragmas, or other preprocessor
  88.       directives.  Some compilers allow code as such:
  89.  
  90.         #ifdef DEBUG
  91.           #define TEST 1
  92.         #endif
  93.  
  94.       or
  95.  
  96.         #ifdef FINAL
  97.         #  ifdef DEBUG
  98.         #    define TEST 1
  99.         #  endif
  100.         #endif
  101.  
  102.       But by no means all.  Use white space if needed to delineate
  103.       #ifdef/endif blocks and comment liberally:
  104.  
  105.         #ifdef FINAL
  106.  
  107.         #ifdef DEBUG
  108.         #define TEST 1
  109.         #endif /* DEBUG*/
  110.  
  111.         #endif /* FINAL */
  112.  
  113.   --  Do not use the ANSI string literal concatenation features.  eg.
  114.  
  115.         printf ("This is ANSI but"
  116.         "unportable code.\n");
  117.  
  118.       for long string literals.  Under ANSI the the compiler should
  119.       concatenate the two string literals into one, but not all ANSI
  120.       compilers support this feature yet.  If you need a very long
  121.       string literal use a form as so:
  122.  
  123.         printf ("%s%s",
  124.         "this is",
  125.         "portable code.\n");
  126.  
  127.       or just use a very long string literal.
  128.  
  129.   --  Stay away from ints except for trash and temp values.  Ints vary
  130.       in size depending upon the memory model under DOS, and legally
  131.       may be any size between shorts and longs inclusive.
  132.  
  133.       Try to use shorts or longs if possible, as these are of fairly
  134.       constant size on most platforms.  On most platforms, but by no
  135.       means all, shorts are usually words and longs word pairs.
  136.  
  137.   --  Beware of assigning a pointer of one type to a pointer of a
  138.       higher type.  Most platforms seem to insist that the addresses
  139.       stored in pointers are alligned per the pointer's base type.
  140.  
  141.       What this means is that the value stored in a pointer to a long,
  142.       in itself will be alligned as a long is.  If longs are alligned
  143.       on even word boundaries, then so will the value of long pointer.
  144.  
  145.       This can result in memory allignment errors which can be
  146.       extremely difficult to track down.  Casting will not help.
  147.  
  148.       DOS has few memory allignment requirements, but for Unix and VMS
  149.       you can expect types to be alligned to their sizes (see the
  150.       compiler manuals for specifics).  What this means to pointers is
  151.       that with code such as:
  152.  
  153.         short *pj2_value;  /* Assume that shorts are alligned to words */
  154.         long *pj4_number;  /* and longs to even word boundaries */
  155.  
  156.         pj4_number = pj2_value;
  157.  
  158.       that the value assigned to pj4_number may be as far as two bytes
  159.       different from that in pj2_value.  ie
  160.  
  161.         Let's say that the address stored is pj2_value is:
  162.  
  163.           *pj2_value == 0000:0006 /* Alligned to word */
  164.  
  165.         and you make the assignment:
  166.  
  167.           pj4_number = pj2_value;
  168.  
  169.        After this, the value of pj4_number will be either 0000:0004,
  170.        or 0000:0008 to shift it to even word allignment.  The
  171.        direction of the shift seems to be compiler/implementation
  172.        dependant.
  173.  
  174.        This bug is often erratic at runtime depending upon the
  175.        allignment of automatics.  This sort of bug is especically
  176.        difficult to track when coming up from a void pointer.
  177.  
  178.   --  Be wary of relying on memory allignment in structures and
  179.       unions.  Different compiler implmentations allign differently,
  180.       and #pragmas or command line arguments to the compiler can
  181.       change allignment at compile time.  See the compiler manuals for
  182.       specific details.
  183.  
  184.       This will usually require you to either read in each member
  185.       individually, or to perform explicit padding when reading
  186.       structure data from disk.  eg. lic1.c & v_lic_pad() in the Axiom
  187.       library.
  188.  
  189.   --  Where possible use sizeof(identifier) rather than sizeof(type)
  190.       or a #defined constant. This can help in tracing down bugs and
  191.       makes for greater readability.  eg.
  192.  
  193.           #define M_DATA 100
  194.           short aj2_numbers[M_DATA];
  195.  
  196.         /* This example requires the reader to remember that
  197.         aj2_numbers has M_DATA elements, is overly complex, and
  198.         presumes that aj2_numbers will always be shorts.  This code
  199.         will likely break if anything is later changed. */
  200.  
  201.           memcpy (aj2_numbers, pj2_input, M_DATA * sizeof (short));
  202.  
  203.         /* This is ideal -- sizeof(aj2_numbers) will return the total
  204.         space allocated to the array, no matter what the type may be,
  205.         or what is changed later.  It makes no assumptions of the
  206.         reader. */
  207.  
  208.           memcpy (aj2_numbers, pj2_input, sizeof (aj2_numbers));
  209.  
  210.   --  Beware of comparing structures or unions with functions such as
  211.       memcmp() as the padding/allignment spaces will have random and
  212.       likely different values.  If you need to compare structures
  213.       you'll have to do it member by member.
  214.  
  215.   --  Never assign structures to each other directly.  Some compilers
  216.       allow structure assignments, some don't.
  217.  
  218.         struct t_data s_struc1;
  219.         struct t_data s_struc2;
  220.  
  221.         s_struc2 = s_struc1; /* Unportable code */
  222.  
  223.       Note however that you can copy structures via pointers as need
  224.       be:
  225.  
  226.         struct t_data *ps_struc1;
  227.         struct t_data *ps_struc2;
  228.  
  229.         ps_struc1 = xxx;
  230.         ps_struc2 = yyy;
  231.  
  232.         *ps_struc2 = *ps_struc1; /* Copy structure 1 to 2 */
  233.  
  234.       Rather than assigning each member over individually.  The
  235.       functions memcpy() and memmove() are other portable ways.
  236.  
  237.   --  Beware of passing chars or shorts to functions.  These get
  238.       promoted to ints, and with some compilers problems from there on
  239.       out abound horrendously.  This is especially true of chars where
  240.       some compilers occassionally extract the wrong byte from the
  241.       promoted int in the recieving function.
  242.  
  243.   --  Be careful passing floats to functions as some compilers promote
  244.       floats to doubles when passing them as an argument.  This can
  245.       cause spurious warnings and and strange side effects.
  246.  
  247.   --  Explicitly cast assignments and expressions as needed, and
  248.       carefully watch that you really don't need to make those
  249.       identifiers of that type originally.  While the promotion order
  250.       is constant across implementations, the size of the types
  251.       aren't.  This can cause difficult side effects.
  252.  
  253.       If your code needs a lot of casts to get past Lint, then you
  254.       probably need to rethink some of your approaches.
  255.  
  256.   --  Take care to cast all #defined constants if in doubt.  For large
  257.       values (longs), always cast as longs, or place an 'L' at the end
  258.       of the constant.  Some compilers handle this area erraticly if
  259.       left up to them.
  260.  
  261.   --  Never ever rely on order of evaluation of function parameters.
  262.       This can occur when listing a funtion call as a parameter to
  263.       another funtion, or as an unintentional side effect from passing
  264.       assignments or function calls as parameters.
  265.  
  266.         char *pc_modify (char *);
  267.  
  268.         printf ("This string [%s] becomes [%s]\n",
  269.         pc_string, pc_modify (pc_string)); /* Bad code */
  270.  
  271.   --  Do not use NULL for anything but pointers.  Do not use NULL for
  272.       string terminations: use the ASCII constant NUL, '\0', or a
  273.       #defined type which equates to that.
  274.  
  275.   --  Never EVER pass a #defined macro an incremented or decremented
  276.       value (++,or  --) or an assignment as a parameter.  This is
  277.       because many #defined macros may reference their arguments
  278.       multiple times.  This is especially true of the macros #defined
  279.       in ctype.h.
  280.  
  281.       eg.
  282.  
  283.         #define iscsymf(c)   (isalpha(c) || ((c) == '_'))
  284.  
  285.      If called as so:
  286.  
  287.        iscsym(var++);
  288.  
  289.      it will be expanded by the preprocessor to:
  290.  
  291.         (isalpha(var++) || ((var++) == '_'))
  292.  
  293.      with var being incremented twice.
  294.  
  295.   --  Avoid passing any functions incremented or decremented values
  296.       ("++" or "--"), or assignments.  Some standard and commercial
  297.       library calls are actually #defined macros.  This type of
  298.       "error" can lead to order of evaluation problems as different
  299.       compilers process function arguments in different orders.
  300.  
  301.   --  Explicitly initialise pointers.  Do not rely on calloc(),
  302.       memset() or other such functions to initialise pointers.
  303.       Initialise them explicitly.
  304.  
  305. "K&R" as mentioned above refers to "The C Programming Language"
  306. written by Brian W Kernighan & Dennis M Ritchie.
  307.  
  308. "PC-Lint" is a commercial version of Lint for the PC, as sold by
  309. Gimpel Software.  PC-Lint is generally acknowledged as the tightest
  310. and most discerning Lint on any platform.
  311.  
  312. There are several books which cover the areas of portable code which
  313. may also help:
  314.  
  315.       "C Programming Guidelines"
  316.       by Thomas Plum
  317.       pub. by Plum Hall
  318.       ISBN 0-911537-03-1
  319.  
  320.       "Portability and the C Language"
  321.       by Rex Jaeschke
  322.       pub. by Hayden Books
  323.       ISBN 0-672-48428-5
  324.  
  325.       "Portable C Software"
  326.       by Mark R. Horton
  327.       pub. by Prentice Hall
  328.       ISBN 0-13-868050-7
  329.  
  330.       "Portable C"
  331.       by Henry Rabinowitz & Chaim Schaap
  332.       pub. by Prentice Hall
  333.       ISBN 0-13-685967-4
  334.  
  335.       "Portable C and Unix System Programming"
  336.       by J.E. Lapin
  337.       pub. Prentice-Hall
  338.       ISBN 0-13-686494-5
  339.  
  340. [END]
  341.