[Rd] Problems with calloc function.

Hin-Tak Leung hin-tak.leung at cimr.cam.ac.uk
Thu Jan 5 17:05:54 CET 2006


Hi Marcelo,

You need to read the R extension manual more carefully...
Basically you haven't deference thed pointers. You think you
were allocating say, col=2, but instead you were allocating &col
in int's, the address of col, which is a large number since
user-land memory address starts at a large offset (0x40000000? =
1/2 GB or 0x80000000 = 1GB?),
so after 4 large allocations, you run out of memory.

Everything through the C interface is passed by pointer, in the
fortran convention.

BTW, you should use Rprintf() instead of printf(). Details below.

Hin-Tak Leung

Marcelo Damasceno wrote:
> Hello all and Prof. Brian Ripley ,
> 
> Sorry about my incautiousness, I use the tips, but is happen same problems.
> Below the R code.
> ################################################################
> rspcplot <- function(file1="dg_01.lab.txt"){
>   if(is.null(n))
>     stop("You need first run the function cluster")
>     file<-"dg_01.lab.txt"
>     aux<-file1
>     file1<-pmatch(file1,file)
>   if(is.na(file1)){
>       matrix=loadMatrix(file,n)
>   }
>   else{
>       matrix=loadMatrix(aux,n)
>   }
>   matrixc<-correct(matrix)
>   #merge2(matrixc)
>   nrow=nrow(matrixc)
>   ncol=ncol(matrixc)
>   ntemp=getTemp()
>   out <- .C("merge2",matrixc,nrow, ncol,ntemp,outMerge=as.integer
> (0),outHeight=as.integer(0),PACKAGE="rspc")
> ##########################################################################
> Below the C code.
> ##########################################################################
> void merge2(int *nmat,int nrow,int ncol, int *ntemp,int ntam, int *out, int
> *height){

Here, you should have "*ncol" instead of "ncol". (I am only correcting 
this one - you can change the others) like this:

	void merge2(int *nmat,int nrow,int *ncol, int *ntemp,int ntam,      int 
*out, int *height){

>     int row,col,*temp,i,j,k,n3,tam,x,aux2,n1;
>     row = nrow;
>     col = ncol;

You should use here:

	int col = *ncol;

> 
>     int *temp1,*temp2,*temp3,*temp4;
> 
>     temp1 = (int*)Calloc(col,int);


inserting here:

	Rprintf("I am trying to allocate col = %d\n", col);

would have told you what's wrong with your code...

>     printf("OK1 \n");


>     temp2 = (int*)Calloc(col,int);
>     printf("OK2 \n");
>     temp3 = (int *)Calloc(col,int);
>     printf("OK3 \n");
>     temp4 = (int *)Calloc(col,int);
>     if(temp4 == NULL){
>         printf("\n\n No Memory4!");
>         exit(1);
>     }
>     printf("OK4\n");
>     int *cvector;
>     cvector = (int *)Calloc(col,int);
>     if(cvector == NULL){
>         printf("\n\n No Memory5!");
>         exit(1);
>     }
>     printf("OK5\n");
>     tam=ntam;
> #######################################################################
> Output of Work Space:
> 
> 
>>rspcplot()
> 
> Read 525 items
> Read 101 items
> OK1
> OK2
> OK3
> OK4
> Error in rspcplot() : Calloc could not allocate (145869080 of 4) memory
> 
> 
> Using the Ruspini data, the values of variables col = 2 and row = 75. I was
> thinking that the number of pointers and space of memory are too big.
> 
> 
> Thanks All !
<earlier posts snipped>



More information about the R-devel mailing list