home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / c / 11604 < prev    next >
Encoding:
Internet Message Format  |  1992-07-25  |  4.4 KB

  1. Path: sparky!uunet!sun-barr!ames!agate!dog.ee.lbl.gov!horse.ee.lbl.gov!torek
  2. From: torek@horse.ee.lbl.gov (Chris Torek)
  3. Newsgroups: comp.lang.c
  4. Subject: prototype and old-style types (was Really weird C warning)
  5. Date: 26 Jul 1992 00:06:37 GMT
  6. Organization: Lawrence Berkeley Laboratory, Berkeley
  7. Lines: 84
  8. Message-ID: <24896@dog.ee.lbl.gov>
  9. References: <4009.107.uupcb@cutting.hou.tx.us>
  10. Reply-To: torek@horse.ee.lbl.gov (Chris Torek)
  11. NNTP-Posting-Host: 128.3.112.15
  12.  
  13. In an article whose referent was deleted by deficient news software,
  14. someone whose initials are presumably DJP (article
  15. <4009.107.uupcb@cutting.hou.tx.us> used `DJP>' as a quote indicator)
  16. wrote:
  17.  
  18. >For the sake of argument, suppose sizeof(int) == 2, sizeof(long) == 4,
  19. >sizeof(char *) == 4, and I have a compiler that aggresively conserves
  20. >stack space. ... If [a function F taking, in order: long, char, char *]
  21. >is declared as a K&R function, the size of the argument vector thus
  22. >passed would then be 4 + 2 + 4 == 10.  (Remember, longs remain long, and
  23. >chars are promoted to int.)  Alternately, if F is declared with a
  24. >prototype, the compiler is completely within its rights to allocate
  25. >the argument vector as 2 + 1 + 4 == 7 ....  The long is converted to int.
  26. >The char remains a char.
  27.  
  28. (Some context appears to be missing, as the long => int conversion should
  29. occur only if the first formal parameter to F has type `int' and there is
  30. a prototype in scope.  The material I inserted (in [] above) presumably
  31. differs from DJP's original, or else this is a mistake and the size should
  32. be 4+1+4 or 9.)
  33.  
  34. In article <4009.107.uupcb@cutting.hou.tx.us>
  35. matt.johnson@cutting.hou.tx.us (Matt Johnson) writes:
  36. >Huh?  This I gotta see to believe.  Don't think there is a Push AL
  37. >instruction for the 80x86 family...
  38.  
  39. Since this is comp.lang.c, not an 80x86-specific newsgroup, bringing up
  40. particular instructions is generally unwise.  Nonetheless, one can
  41. easily imagine a compiler that uses `movb' to put char arguments on the
  42. IBM PC stack, having allocated the proper number of bytes using simple
  43. subtraction.
  44.  
  45. In any case, DJP's point stands as the primary motivation behind the
  46. rules in ANSI C for mixing prototypes and K&R-style definitions.  Or
  47. rather, it does once it is augmented with a bit of history.
  48.  
  49. Classic C promoted arguments always.  If you attempted to pass a char,
  50. short, or float value to a function, that function actually *got* an
  51. int or a double.  A number of vendors objected on the grounds that
  52. avoiding promotion (and subsequent demotion) was unnecessarily
  53. inefficient.
  54.  
  55. The ANSI C committee decided, then, to allow compilers to omit any
  56. unnecessary promotion-and-demotion code IF AND ONLY IF *both* the
  57. caller and the callee used New C conventions.  The caller must have a
  58. prototype in scope; this gives the compiler license to send arguments
  59. without first promoting char, short, and float.  Similarly, the
  60. callee---the destination function---must use the new prototype-like
  61. definition syntax.
  62.  
  63. As an aid toward catching mistakes, the ANSI C committee decided that,
  64. if the compiler sees a function definition while a prototype for that
  65. function is also in scope, it must check that each formal parameter has
  66. an `unpromotable' type.  Thus, given
  67.  
  68.     void f(char);        /* this is a new-style declaration */
  69.  
  70.     void f(c)        /* this is an old-style definition */
  71.         char c;
  72.     {
  73.         /* your code goes here */
  74.     }
  75.  
  76. an ANSI C compiler must issue at least one diagnostic, because the
  77. definition claims that f() should receive one `int'---the type of
  78. `char' after promotion---but the prototype claims that f() should
  79. receive one `char'.
  80.  
  81. It has been observed on this group before that MS C 6.0 gets this much
  82. right, but gets something else wrong:  It comes with a program (the C
  83. compiler, actually) that purports to generate correct prototypes from
  84. correct old-style code.  This program actually generates incorrect,
  85. incompatible prototypes, and the C compiler correctly complains about
  86. clashes between these prototypes and the old-style definitions.
  87.  
  88. It seems a bit peculiar that a single program, albeit a complex one
  89. with multiple modes of operation, should be inconsistent in this way.
  90. It gets the diagnostic right but emits incorrect prototypes.  The most
  91. likely explanation, Microsoft being Microsoft, is that two different
  92. programmers handled the different modes, and only one read the ANSI C
  93. standard.
  94. -- 
  95. In-Real-Life: Chris Torek, Lawrence Berkeley Lab CSE/EE (+1 510 486 5427)
  96. Berkeley, CA        Domain:    torek@ee.lbl.gov
  97.