Subject: example for k&r being bad (was: Re: Bluetooth protocol code)
To: Bill Studenmund <wrstuden@netbsd.org>
From: Hubert Feyrer <feyrer@cs.stevens.edu>
List: tech-kern
Date: 12/20/2005 06:27:54
[Please drop tech-kern@ on replies, I think this belongs on tech-userlevel@]


On Mon, 19 Dec 2005, Bill Studenmund wrote:
> The problem is that K&R prototypes won't work right if we pass in a
> parameter that's larger than an int. Like a long or a pointer on an LP64
> system.
>
> In such a case, the caller will turn the parameter into an int, truncating
> it. Worse yet, the caller will then pack other parameters in, not leaving
> enough space for what the caller will expect the parameter to be. Thus the
> callee will see two parameters mixed together in what it thinks is one
> parameter (by our style guide, it will get the parameters right), and a
> parameter at the end will be totally uninitialized.
>
> This behavior is why we're moving away from K&R parameters & prototypes.

I've tried to write a program that shows this, but didn't succeed.
The program I come up with (based on your description) is available at 
[1], output on ILP32 (NetBSD/i386) is in [2], output on LP64 
(NetBSD/alpha) is in [3]. Obviously they are the same, so I must have 
missed something. What is it?

I think it would be useful to have a smallish piece of code that we can 
point people at and say "look, differences between architectures if 
you use K&R"...


  - Hubert

[1] http://www.cs.stevens.edu/~feyrer/lp64.c
[2] http://www.cs.stevens.edu/~feyrer/lp64-output-i386
[3] http://www.cs.stevens.edu/~feyrer/lp64-output-alpha