Skip to content
Prev 15742 / 63461 Next

How allocate STRSXP outside of gc

On Tue, Apr 12, 2005 at 12:31:03PM -0700, Vadim Ogranovich wrote:
For being unaffected by R's memory management, it may be the best to
not use a SEXP for storing the pre-computed result at all. Rather, use
a static variable "private" to your code, as in

    SEXP getFoo()
    {
      static char *foo = NULL;

      if (foo == NULL)
      {
        foo = the_difficult_to_compute_value_of_foo();
      }
      return mkChar(foo);
    }

This way, getFoo indeed invokes mkChar each time, but in your scenario,
that might be an overhead which is negligible compared to the actual
computation of the foo value.

Best regards, Jan