[Rd] memory allocation questions

Duncan Murdoch murdoch at stats.uwo.ca
Tue Sep 14 15:03:20 CEST 2004

On Tue, 14 Sep 2004 09:38:55 +1000, <Mark.Bravington at csiro.au> wrote :

>Dear R-devel
>This one seems a bit arcane for R-help. I very often use R to call routines written in Delphi (son of Pascal), doing "persistent" memory allocation within Delphi. That is, I start R and load the Delphi DLL; then I use .C to call a Delphi routine which allocates a piece of memory X and returns to R a pointer to X; then I do some more stuff in R; then I call another Delphi routine, passing it the pointer to X. Finally, I call another Delphi routine from R to free the memory at X.
>For years, I've used Delphi's own memory manager for this (the basic one, not SHAREMEM.DLL), but recently I've been wondering whether it would be better to use R's wrappers for "alloc" etc. instead. The Delphi routines seem generally to work fine, but my nagging concern [based on ignorance] is whether R's memory manager will somehow not realize that Delphi has already allocated the memory, and will overwrite it inadvertently. It's fairly easy to replace Delphi's manager with the R routines, following the instructions in section 5.1.2 of R-EXTS and the Delphi manual. So my first question is: 
>should I bother using the R routines instead?

There is not much reason to need to use the R routines.  The
disadvantages of using Delphi allocation are:

  - Your allocations won't be automatically deleted by the garbage
collector (but neither are those of Calloc, you need R_alloc for that)
  - If allocation fails, it will be up to you to detect that and deal
with it.

The disadvantage of using the R routines Calloc etc. is that
  - You need to translate C macros to Delphi.
  - You need to worry about whether the RS.h file has changed with
each release (but the Calloc etc. macros are likely to be very stable,
they haven't changed since 2000).

Duncan Murdoch

More information about the R-devel mailing list