s-news
[Top] [All Lists]

Re: C call - memory problem

To: Agin.Patrick@hydro.qc.ca
Subject: Re: C call - memory problem
From: Prof Brian Ripley <ripley@stats.ox.ac.uk>
Date: Wed, 31 May 2006 22:13:16 +0100 (BST)
Cc: david.jessop@ubs.com, s-news@lists.biostat.wustl.edu
In-reply-to: <30258329AD9B574EB1BBFF02CFB7E66B907869@WPMSXC04.hydroqc.hydro.qc.ca>
References: <30258329AD9B574EB1BBFF02CFB7E66B907869@WPMSXC04.hydroqc.hydro.qc.ca>
First, S-PLUS 4.5 is a very different system from current versions, built from a different codebase with a different compiler (Watcom). People have been talking at cross-purposes here.

Second, it is legal C to declare a C function without a prototype. A function with no arguments is declared as foo(void), not as foo(). You will run into problems if you use such C headers in a C++ compiler. My guess is that the compiler (note: said to be VC++) is being used as a C++ compiler.

Third, there is an explanation of how the memory allocation functions have changed over the years in S Programming, with macros to smooth over the differences. But it really is a long time since S-PLUS 4.5 or 2000 (the last of that series) were current.

On Thu, 25 May 2006, Agin.Patrick@hydro.qc.ca wrote:

Sorry Dave... but I have no such files under my C:\Program
Files\splus45\include directory. When I grep for S_alloc, only one
result is found and it is the one I mentionned in my last email (file
S.h).  By the way, I found something on the net (June 1998) that seems
to be related to my problem. It is a message from Oliver Klinger
(klinger@lkn.e-technik.tu-muenchen.de) named "[S] Corrupt C/C++
Macros???". I put the contents of this message below, maybe it will
remind to someone an answer that was posted at that time.

Patrick

**************************************************



When programming under MS Visual C++ 5.0 it's impossible to access any
of the Macros defined in <S.h>
Up to now I encountered problems with the follwoing macros (I'm sure
there are some more problems to follow, if I tried other mcros):
S_alloc(),
Calloc(),
PRINT_IT(),
MESSAGE(),
WARNING(), and
RECOVER().

The compiler alwas ends with the error: "Too many arguments in function
xxx"

My include-section looks as follows:

# if defined DLL_LOAD
# include <windows.h>
BOOL __stdcall
DllMain (HINSTANCE hDllInstance, DWORD dwReason, LPVOID lpReserved) {
switch (dwReason) {
case DLL_PROCESS_ATTACH:
break;
case DLL_PROCESS_DETACH:
break;
}
return(TRUE);
}
#endif
# include <S.h>
# include <newredef.h>
# include <math.h>
# include <string.h>
# include <stdlib.h>





________________________________

From: david.jessop@ubs.com [mailto:david.jessop@ubs.com]
Sent: Thursday, May 25, 2006 9:57 AM
To: Agin, Patrick; s-news@lists.biostat.wustl.edu
Subject: RE: C call - memory problem



Patrick,



Try looking in S_extern.h and S_define.h where in my version (6.2) I
find



S_define.h(28): #define Salloc(n, t) (t *)S_alloc(n,
sizeof(t),S_evaluator)
S_extern.h(66): LibExtern void * S_STDCALL S_alloc(long n, size_t size,
s_evaluator *S_evaluator);

If I look in my language reference I find exactly the same as you do.



David






________________________________


        From: Agin.Patrick@hydro.qc.ca [mailto:Agin.Patrick@hydro.qc.ca]

        Sent: 25 May 2006 14:32
        To: Jessop, David; s-news@lists.biostat.wustl.edu
        Subject: RE: C call - memory problem

        Excuse me David, I forgot to mention in my last message one
thing. When I say that S_alloc has two arguments, it's because I found a
topic on that function in the help of the S-Plus language reference. In
my version 4.5, here is the usage : char *S_alloc(nelem, elsize). I
suppose that the 3rd argument came in later versions only. The question
remains : how can I plug the arguments if the compiler expects no one?

        Patrick




________________________________


        From: david.jessop@ubs.com [mailto:david.jessop@ubs.com]
        Sent: Thursday, May 25, 2006 6:27 AM
        To: Agin, Patrick; s-news@lists.biostat.wustl.edu
        Subject: RE: C call - memory problem



        Patrick,



        I think you've come up against the famous "not quite accurate
documentation" problem.  I'm running with a later version than you, but
my version of S needs three parameters, e.g.:



         char *pcName = (char *) S_alloc (iMaxNameLen, sizeof (char),
S_evaluator);

        I only found this by searching through the header files with
grep (or your favourite text search routine) for the prototype.



        Hope this helps,



        David






________________________________


                From: Agin.Patrick@hydro.qc.ca
[mailto:Agin.Patrick@hydro.qc.ca]
                Sent: 24 May 2006 20:04
                To: s-news@lists.biostat.wustl.edu
                Subject: C call - memory problem

                Dear all,



                I built a DLL (under VC++) and loaded it in S-Plus
(version 4.5, Windows XP) with dll.load(). When I do the call to my C
function, S-Plus crashes and the following message appears:



                SPLUS.EXE - Application Error

                The instruction at {xxx}

                 referenced memory at {yyy}. The memory could not be
"read".



                The crash occurs between the end of the C function and
the return to S-Plus. I suspect problems with the malloc/free calls so I
wanted to replace them with S_alloc calls only but the compiler does not
like it: "S_alloc function does not take 2 arguments" yet I included the
header <S.h> as mentionned in the Programmer's Guide. What am I doing
wrong? Is the problem elsewhere? What could it be? I must admit that
after reading topics in the S-news about C calls from S-Plus, I'm quite
confused because some elements seem to apply only to more recent
versions of my "good-old" S-Plus 4.5.



                Any hints would be very appreciated.

                Patrick











--
Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

<Prev in Thread] Current Thread [Next in Thread>