home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / C_PORT.TXT < prev    next >
Text File  |  1997-07-05  |  13KB  |  343 lines

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