new style declarations OK for old style definitions? 
Author Message
 new style declarations OK for old style definitions?

K&R/2 is a little vague on the following question: under what
circumstances is it legal to declare using new-style syntax a
separately compiled function that was compiled with an old-style
definition.

Is it sufficient to use only promoted arguments in the new-style
declaration?

Conceivably, the whole calling sequence for old style and new style
definitions could differ. Is it legal for the compiler to choose
completely incompatible calling sequences for old-style and new-style
declarations? It would certainly be desirable, since new-style
declarations seem to allow for significant optimizations that aren't
possible with old-style declarations.

This is a frequent problem if you have old library binaries and
sources but want the extra type checking from an ANSI compiler.

                                Thanks, Thomas.



Sun, 19 Sep 1993 19:17:24 GMT  
 new style declarations OK for old style definitions?

Quote:

>K&R/2 is a little vague on the following question: under what
>circumstances is it legal to declare using new-style syntax a
>separately compiled function that was compiled with an old-style
>definition.

>Is it sufficient to use only promoted arguments in the new-style
>declaration?

Yes. If you don't use the default promotion types you will have
problems. If you have a old-style definition of:

int xyz(a,b,c)
        char  a;
        short b;
        float c;
{
        return 0;

Quote:
}

The new-style declaration is:

extern int xyz(int a, int b, double c);

Quote:
>Conceivably, the whole calling sequence for old style and new style
>definitions could differ. Is it legal for the compiler to choose
>completely incompatible calling sequences for old-style and new-style
>declarations?

Yes. If you said one thing and did another the compiler would have no way
of known that you were lying to it, unless the new-style declaration was also
in the same file as the old-style definition.

+------------------------------------+---------------------------------+
| Geoffrey C. Rogers                 | "Whose brain did you get?"      |

| {sun,uunet,uiucdcs}!convex!grogers |                                 |
+------------------------------------+---------------------------------+



Sun, 19 Sep 1993 22:18:49 GMT  
 new style declarations OK for old style definitions?

Quote:
>K&R/2 is a little vague on the following question: under what
>circumstances is it legal to declare using new-style syntax a
>separately compiled function that was compiled with an old-style
>definition.
>Is it sufficient to use only promoted arguments in the new-style
>declaration?

You must not use char, short or float arguments in any of the
declarations (nor in the ultimate function header).  The function
must not accept a variable number of parameters, and the prototype
must not include the ellipsis (...).  These rules will allow
portability of the code.

Quote:
>Conceivably, the whole calling sequence for old style and new style
>definitions could differ. Is it legal for the compiler to choose
>completely incompatible calling sequences for old-style and new-style
>declarations?

Not when the above rules are followed.  If you violate any of these rules,
a conforming compiler is allowed to use different calling sequences.

Quote:
>It would certainly be desirable, since new-style
>declarations seem to allow for significant optimizations that aren't
>possible with old-style declarations.

Yes.  Prototyped functions may use different parameter passing for
chars, shorts, and floats, and would do so if they are more efficient.
In particular, when a function is declared with an ellipsis, the compiler
may use a different method of passing the extra parameters than if you
call a non-prototyped function.

Quote:
>This is a frequent problem if you have old library binaries and
>sources but want the extra type checking from an ANSI compiler.
>                            Thanks, Thomas.

--




Tue, 21 Sep 1993 00:40:04 GMT  
 new style declarations OK for old style definitions?

|> K&R/2 is a little vague on the following question: under what
|> circumstances is it legal to declare using new-style syntax a
|> separately compiled function that was compiled with an old-style
|> definition.
|>
|> Is it sufficient to use only promoted arguments in the new-style
|> declaration?
|>
|> Conceivably, the whole calling sequence for old style and new style
|> definitions could differ. Is it legal for the compiler to choose
|> completely incompatible calling sequences for old-style and new-style
|> declarations? It would certainly be desirable, since new-style
|> declarations seem to allow for significant optimizations that aren't
|> possible with old-style declarations.
|>

K&RII says (I'm reciting from memory, my book's at home):

If a function which is declared new-style is called, and
no prototype for it exists in the current scope, the
promoted type of the actual argument must agree exactly
with the unpromoted type of the formal argument.

Since the compiler could not know whether the function
was declared new-style or old-style (burp :-), it must
use the same calling conventions for both.

you are asking the converse, ig you can call an old-style function
with a (new-style) prototype existing for it in the current scope, but I
believe it is implicitly specified that using a prototype
with promoted arguments is O.K.

My question is why?  just eliminate the prototype, or if
you need the parameter type checking, change the definition
of your function to be new-style, and change all of the formal
arguments to their promoted types.  Then you are following
the ANSI standard to the letter.

bb



Tue, 21 Sep 1993 04:34:18 GMT  
 new style declarations OK for old style definitions?

Quote:

>Conceivably, the whole calling sequence for old style and new style
>definitions could differ. Is it legal for the compiler to choose
>completely incompatible calling sequences for old-style and new-style
>declarations?

No, a conforming implementation must support intermixing of old-style
and prototype-style function linkage, so long as compatible types are
involved.  Basically that means that an exception can be made for
prototypes only when at least one parameter or the return value has a
type different from those obtained by default widening conventions,
and also the linkage can differ for the ",..." style of prototype no
matter what the types of the fixed arguments.


Tue, 21 Sep 1993 01:48:22 GMT  
 new style declarations OK for old style definitions?
The original question was about mixing an old-style, unprototyped
function definition in one file with a new-style, prototyped
declaration in another.


Quote:
(Geoffrey Rogers) writes:


   > Is it sufficient to use only promoted arguments in the new-style
   > declaration?

   Yes. If you don't use the default promotion types you will have
   problems.

Exactly.

   >Is it legal for the compiler to choose
   >completely incompatible calling sequences for old-style and new-style
   >declarations?

   Yes.

No.  Section 3.5.4.3 of the Standard defines compatability of function
types.  An old-style function declaration is compatable with a
new-style declaration, if the new-style uses promoted arguments and
doesn't use ellipses.

Thus, with care, you CAN mix new-style with old-style.  Because of the
care required, I do not recommend such mixing.

--
   "Of course he has a knife.  We all have knives.  It's 1183, and we're
   all barbarians."
Tim McDaniel                 Applied Dynamics Int'l.; Ann Arbor, Michigan, USA



Fri, 24 Sep 1993 22:56:31 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. old style casts vs new style casts

2. old style casts vs new style casts

3. old C style to new C style conversion

4. Prototypes with old style definitions

5. prototypes with old style fn definition

6. Prototyping old style fn declarations.

7. Convert old-style func decl to ANSI-style?

8. Converting old style functions to new

9. Conversion between old and new-style source code

10. Old style fucntion declarations and definitions supported with ISO C99?

11. Old Style fns. (vs) New Style fns.

12. Old style C to ANSI C

 

 
Powered by phpBB® Forum Software