From roebuck at mdanderson.org  Tue Feb 21 07:14:37 2006
From: roebuck at mdanderson.org (Paul Roebuck)
Date: Tue, 21 Feb 2006 00:14:37 -0600 (CST)
Subject: [Rd] simple C function segfaults
In-Reply-To: <20060221044245.33844.qmail@web27412.mail.ukl.yahoo.com>
References: <20060221044245.33844.qmail@web27412.mail.ukl.yahoo.com>
Message-ID: <Pine.OSF.4.58.0602202348210.91966@wotan.mdacc.tmc.edu>

On Tue, 21 Feb 2006, Bernd Kriegstein wrote:

> I use the simplest of examples that somebody can think
> of in order to generate a matrix of random numbers
> from within C, calling appropriate R functions. The
> concrete example is below:
> [SNIP]

------ pico.c --------
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <R.h>
#include <Rmath.h>

#define COLM(i,j,m) ((m * j) + i)

void pico(double *y, int *n, int *m) {
    register int i;

    GetRNGstate();
    for (i = 0; i < *n; i++) {
        register int j;

        for (j = 0; j < *m; j++) {
            y[COLM(i,j,*m)] = rnorm(0.0, 1.0);
        }
    }
    PutRNGstate();
}

------ pico.R --------
dyn.load("pico.so")
n <- 10
m <- 5
ans <- .C("pico",
          as.double(matrix(0, n, m)),
          as.integer(n),
          as.integer(m))
str(ans[[1]])

----------------------

$ R CMD SHLIB pico.c
$ R --vanilla < pico.R

----------------------------------------------------------
SIGSIG -- signature too long (core dumped)



[Rd] simple C function segfaults
Paul Roebuck