[Rd] Calloc() & Free() in different .C(.) calls --> not usable ?

Martin Maechler Martin Maechler <maechler@stat.math.ethz.ch>
Thu, 7 Sep 2000 18:32:59 +0200 (CEST)


       [sending this to R-devel, since it may be of general interest and 
        could possibly lead to better instructions in the "R-exts" manual]

Is this another case where only .Call() or .External() [or a fudge factor]
solve my problem? 

Following situation: 
 I use dyn.loaded C code to compute a recursive tree structure from my data.
 Beforehand I don't know how big the tree will be, but I want the tree
 (as integer array in my case) be passed back to R.

 Current solution [simplified!]:

  
 ## First I fit the tree to my Data, all in C, but only return the *size*
 ## I'll need to store that tree in R

	 size <-      .C("fittree", data, ..., res = integer(1))$res

 ## Now that I know its size, I can get it into R :

	 tree.in.R <- .C("gettree", size, res = integer(size))$res

 where in the C code, I have a
 global 
	static *tree_struct my_tree;
 and in C's fittree(), I use 
	my_tree = Calloc(1, tree_struct); 
 for building the tree, which is 
 then accessed by gettree(), which ends in
	Free(my_tree);

All well, BUT: 
    problem is that memory just builds up and is not freed at all.

>From a (more private) E-mail of Prof Brian Ripley, I read (maybe wrongly?)
that this seems to be ``common'':

    BDR> and the way that most mallocs work is to use sbrk to raise or lower
    BDR> the brk, that is the size of the data space. 
    BDR> They only reduce it when the data space is sufficiently unfragmented. 
	 --------------------------------------------------------------------
which may be my case : "Free(my_tree)" doesn't really free the
memory [according to my tests, looking at Unix' "ps .."].

Solutions (yes, one of them is  ``use .Call() !'') ??

Martin Maechler <maechler@stat.math.ethz.ch>	http://stat.ethz.ch/~maechler/
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._