From rdiaz02 at gmail.com  Sat Apr 26 12:21:13 2008
From: rdiaz02 at gmail.com (Ramon Diaz-Uriarte)
Date: Sat, 26 Apr 2008 12:21:13 +0200
Subject: [Rd] returning vectors of unknown size from C (with .C)
In-Reply-To: <alpine.LFD.1.10.0804261036370.14262@gannet.stats.ox.ac.uk>
References: <624934630804251723n2e6b6345gc1678297e9412d34@mail.gmail.com>
	<4812831D.5070100@stats.uwo.ca>
	<624934630804260232h4afee17ckfbb1ecc0c88112d4@mail.gmail.com>
	<alpine.LFD.1.10.0804261036370.14262@gannet.stats.ox.ac.uk>
Message-ID: <624934630804260321o4fa13e65tf474295d908784b2@mail.gmail.com>

On Sat, Apr 26, 2008 at 11:38 AM, Prof Brian Ripley
<ripley at stats.ox.ac.uk> wrote:
> On Sat, 26 Apr 2008, Ramon Diaz-Uriarte wrote:
>
>
> > On Sat, Apr 26, 2008 at 3:19 AM, Duncan Murdoch <murdoch at stats.uwo.ca>
> wrote:
> >
> > > Ramon Diaz-Uriarte wrote:
> > >
> > >
> > > > Dear All,
> > > >
> > > > In a package, I am using ".C" to call some C functions. In one case,
> > > > the number of elements of the return vectors are not known in R before
> > > > the C call. (Two of the vectors are integers, the third is vector of
> > > > character strings).
> > > >
> > > > Passing from R a vector of the maximum possible size would be a huge
> > > > waste. I understand one alternative is to use ".Call", but I'd rather
> > > > avoid it if I can (all of the code seems working except for the return
> > > > of values into R). Another would be to write to a file from C and then
> > > > read that into R, but this looks very ugly. Are there any other
> > > > reasonable alternatives, or should I just use .Call?
> > > >
> > > >
> > > >
> > >
> > >  .Call is usually easiest, but another possibility is to have two entry
> > > points:  one to calculate how much space you need, a second to pass in a
> > > vector that's the right size to hold the result.
> > >
> > >
> >
> >
> > You mean making two successive calls to the C code? The problem is
> > that the size of the result is not known until the result is obtained
> > (in my C code, the underlying structure is a linked list that gets
> > stretched as needed as the computation proceeds). So I would not know
> > "where to leave the result from C" in between the two calls to C.
> >
>
>  But that is possible (you malloc the memory for a local copy in the rist
> call), and rpart does something like it.
>

Aha, thanks, I didn't know it was doable (or easy). I'll look at the
rpart code. One further question, though, what is "the rist call"?

Thanks,

R.


>
>  --
>  Brian D. Ripley,                  ripley at 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
>



-- 
Ramon Diaz-Uriarte
Statistical Computing Team
Structural Biology and Biocomputing Programme
Spanish National Cancer Centre (CNIO)
http://ligarto.org/rdiaz



[Rd] returning vectors of unknown size from C (with .C)
Ramon Diaz-Uriarte