[Rd] Samples of external code with various compilers?

Duncan Murdoch dmurdoch@pair.com
Tue Dec 3 12:42:03 2002

On Tue, 3 Dec 2002 10:01:08 +1100 , Mark.Bravington@csiro.au wrote:

>what call mode to use for procedures (i.e. stack order and removal of
>parameters; I had always used STDCALL with S and R, and then found I was
>getting bugs with R 1.6.1. So in desperation I eventually changed this at
>random to C-CALL, and things started working again in R-- and continued to
>work in S. To my continued puzzlement, actually.)

The difference between stdcall and cdecl is that in the former, the
routine removes parameters from the stack, whereas in the latter, the
caller does.

R uses cdecl.  When the routine used stdcall, the parameters would be
removed twice.  My guess about why this worked was that the
"do_dotCode" routine had enough redundant locals that having some of
them removed from the stack didn't cause obvious problems.  In 1.6,
do_dotCode was modified, and now it messes up if you steal its locals.

Most other Windows programs use stdcall.  Using the stdcall convention
to call a cdecl routine means that the parameters won't be removed
from the stack by either the caller or the routine.  However, S-PLUS
is probably like R, and only makes one call to your routine from the
function that calls it.  When it returns, the extra junk on the stack
is removed.

Since stdcall is the Windows standard, and R uses cdecl, I've been
thinking lately about whether it would be worth putting in stdcall as
an option to .C and .Fortran.  Possibly we could just switch to
stdcall, and rely on the behaviour in the paragraph above to handle
cdecl routines, but that sounds pretty ugly to me.

Duncan Murdoch