Re: Inconsistent Program Results
In article <1173341327.599 135.112420@p10g 2000cwp.googleg roups.com>
<Francine.Neary @googlemail.com wrote:
Well, yes; but also no.
In particular, if you have a prototype declaration for a function,
and you change the function to take different parameters (but no
change to its return type), you must find and update the prototype.
(The parenthetic remark is to distinguish this case from the one
where you *do* change the return type; in this case, you must
edit any declaration regardless of whether it is also a prototype.)
This is "new work" that you did not have to do if you did not use
a prototype.
On the obverse, however, if you change a function to take different
parameters, you must find and update all calls to that function.
This maintenance *task* is unchanged whether or not you use a
prototype -- but if you *do* use a prototype, any calls that you
miss will [1] draw a diagnostic from any correct [2] C compiler.
This makes the actual maintenance easier: now you can make any
changes you know you need to make, then start a compilation, and
make any additional changes the compiler points out.
There are languages that avoid the need for redundant declarations
like prototypes. Compilers for these languages (if they are in
fact compiled) automatically record the actual type of the actual
definition, and automatically compare calls. In effect, these
compilers build prototypes for you. C is not such a language, but
if you use prototypes consistently, you can get most of this benefit,
at a relatively low cost.
-----
[1] Provided the prototype is in scope at the point of the call,
anyway. Better compilers have flags to warn you if you make a call
without a prototype in scope, so that you can be sure that all
calls have a prototype. If you always put such prototypes in a
"safe" location -- such as a header file that you also include in
the code that defines the function -- then you get consistent and
"safe" checking. If you scatter multiple copies of the prototype
in multiple separate files, however, and/or fail to have an
appropriate prototype in place at the point of the call, you lose
the benefit. This is where those Other Languages with "automatic"
prototyping seem superior, at least up until you have to write the
compiler for one. :-)
[2] "Correct" is kind of a weasel-word for "conforming ". A lot of
modern C compilers will warn even in non-conforming modes, such as
their default modes, so "conforming " is not quite the right adjective
anyway.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
In article <1173341327.599 135.112420@p10g 2000cwp.googleg roups.com>
<Francine.Neary @googlemail.com wrote:
>Surely [using prototypes] makes the code harder to maintain?
In particular, if you have a prototype declaration for a function,
and you change the function to take different parameters (but no
change to its return type), you must find and update the prototype.
(The parenthetic remark is to distinguish this case from the one
where you *do* change the return type; in this case, you must
edit any declaration regardless of whether it is also a prototype.)
This is "new work" that you did not have to do if you did not use
a prototype.
On the obverse, however, if you change a function to take different
parameters, you must find and update all calls to that function.
This maintenance *task* is unchanged whether or not you use a
prototype -- but if you *do* use a prototype, any calls that you
miss will [1] draw a diagnostic from any correct [2] C compiler.
This makes the actual maintenance easier: now you can make any
changes you know you need to make, then start a compilation, and
make any additional changes the compiler points out.
There are languages that avoid the need for redundant declarations
like prototypes. Compilers for these languages (if they are in
fact compiled) automatically record the actual type of the actual
definition, and automatically compare calls. In effect, these
compilers build prototypes for you. C is not such a language, but
if you use prototypes consistently, you can get most of this benefit,
at a relatively low cost.
-----
[1] Provided the prototype is in scope at the point of the call,
anyway. Better compilers have flags to warn you if you make a call
without a prototype in scope, so that you can be sure that all
calls have a prototype. If you always put such prototypes in a
"safe" location -- such as a header file that you also include in
the code that defines the function -- then you get consistent and
"safe" checking. If you scatter multiple copies of the prototype
in multiple separate files, however, and/or fail to have an
appropriate prototype in place at the point of the call, you lose
the benefit. This is where those Other Languages with "automatic"
prototyping seem superior, at least up until you have to write the
compiler for one. :-)
[2] "Correct" is kind of a weasel-word for "conforming ". A lot of
modern C compilers will warn even in non-conforming modes, such as
their default modes, so "conforming " is not quite the right adjective
anyway.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Comment