[R] Problem with C extension

Martin Maechler maechler at stat.math.ethz.ch
Fri Jan 19 18:15:42 CET 2007


>>>>> "Markus" == Markus Schmidberger <schmidb at ibe.med.uni-muenchen.de>
>>>>>     on Fri, 19 Jan 2007 15:02:31 +0100 writes:

    Markus> Hello,
    Markus> I try to write an extension in C, to get a faster functions.
    Markus> Therefore I have to add an element (vector) to a vector. The command in 
    Markus> R is very simple: x = c(x,a)
aka
	x <- c(x,a)

see that's why you'd probably rather stick with R a bit longer,
and profile [-> help(Rprof)] your code and try to speedup quite
a bit before thinking about using C ...

    Markus> But in C I have the problem to reallocate my vector for getting more 
    Markus> space. Everything I tried, I get a "Segmentation fault".

    Markus> So, how can I combine two vectors in C and give the result back to R 
    Markus> (return(x))?

and you have a copy of "Writing R Extensions" right in front of
you, electronically at least, I mean?

To return the new vector via C's return() you'd definitely need
to work with .Call() {which is a good thing but really not for
C-beginners}, and that needs a bit time of reading the above manual "from
cover to cover". Note that you probably should start with (5.8) on
.Call.

I'm also tending to recommend even starting to peek into R's own
C source, notably the header files (src/include/...), and maybe
src/main/* in order to see how R objects ("SEXP"s) are handled,
how you add names(), dimnames() etc internally.. 

Hoping that helps,
Martin Maechler, ETH Zurich

    Markus> Thanks
    Markus> Markus Schmidberger

    Markus> -- 
    Markus> Dipl.-Tech. Math. Markus Schmidberger



More information about the R-help mailing list