[Rd] double pointer matrix
Hin-Tak Leung
hin-tak.leung at cimr.cam.ac.uk
Tue Mar 7 14:37:25 CET 2006
Please don't do malloc inside C code like this - it won't interact too
well with the garbage collector in R, and your program probably will
either leak or crash or both... and when are you going to do your
free()?
What you want is do is to delete that malloc line and do the
allocation on the R side, something like this (the first line does
the equivalent of your malloc allocation):
y <- double(n)
y <- .C("retMat", as.double(y), as.double(n), as.double(m),
as.double(a), as.double(b))[1]
HTL
Bernd Kriegstein wrote:
> Hello,
>
> I'm having some difficulty to understand how I could
> take the proper result from the following listing:
>
> -- cut ---
> #include <stdlib.h>
> #include <R.h>
> #include <Rmath.h>
>
> void retMat ( double **y, int *n, int *m, double *a,
> double *b) {
> int i, j;
> y = malloc( (*n) * sizeof( double ) );
> for (i=0; i<*n; i++) {
> y[i] = malloc ( (*m) * sizeof( double
> ) );
> }
>
> GetRNGstate();
>
> for (i=0; i<*n; i++) {
> for (j=0; j<*m; j++) {
> y[i][j] = (i+1)*(j+1)*rbeta(
> *a, *b );
> }
> }
>
> PutRNGstate();
> }
> ---
> I understand that I will have to make the matrix
> initialized in the double for loop above to be somehow
> visible as a vector, because of the way that the
> matrix elements are passed in the argument when used
> in the R space. Is there a way to accomplish this?
>
> Thanks for any answers,
>
> - b.
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
More information about the R-devel
mailing list