From Torsten.Hothorn at rzmail.uni-erlangen.de  Fri Apr  2 08:39:59 2004
From: Torsten.Hothorn at rzmail.uni-erlangen.de (Torsten Hothorn)
Date: Fri Apr  2 08:40:14 2004
Subject: [Rd] Memory Protection & calling C-fun from C
Message-ID: <Pine.LNX.4.51.0404020838030.8426@artemis.imbe.med.uni-erlangen.de>


Good morning!

The descriptions of memory protection all assume that one
is calling a C-function directly from R. I'm not sure if my
understanding of calling a C-function from another C-function
is correct:

Suppose there are two functions

SEXP bar(SEXP y) {
  SEXP b;
  PROTECT(b = allocVector(...));
     ... computations on b
  UNPROTECT(1);
  return b
}

and

SEXP foo(SEXP x) {
  SEXP a;

  PROTECT(a = bar(x));  /* use bar to do lowlevel computations */
    ... futher computations on a
  UNPROTECT(1);
  return a;
}

Of course,

R> .Call("bar", x)

is safe but is

R> .Call("foo", x)

too? May it happen that the object `b' points to is destroyed before it is
protected by `PROTECT(a = bar(x))'?

I searched for examples of that in R and some packages but did only find
some where `bar' is defined by

   double* bar(double *y)

and the problems do not occur. Anyway, I want to be able to call both
`foo' and `bar' directly from R and C-level (say for the sake of writing
tests in R for the lowlevel functions).

Any clarification is very welcome && Thanks in advance,

Torsten


[Rd] Memory Protection & calling C-fun from C
Torsten Hothorn