[Rd] Problems with calloc function.

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Dec 29 18:40:37 CET 2005


Rather than trying to reinvent the wheel, why not make use of the 
functions documented in Writing R Extensions, especially Calloc/Free?

And definitely do not call exit() from code linked into R: it is 
antisocial behaviour to terminate your host's process.

Your example is incomplete, and for some values of 'col' you will run out 
of memory.  (It is also not legal C as written, since you mix code and 
declarations.)

On Thu, 29 Dec 2005, Marcelo Damasceno wrote:

> Hi all,
>
> I have a C code in Linux, it has 7 pointers and compile e run OK, but when I
> run in R happens problems with calloc function, it returns NULL.
> ###############################################
>> int *temp1,*temp2,*temp3,*temp4;
>
> temp1 = (int *)calloc(col,sizeof(int));
> if(temp1 == NULL){
>     printf("\n\n No Memory1!");
>     exit(1);
> }
>
>  temp2 = (int *)calloc(col,sizeof(int));
>  if(temp2 == NULL){
>        >printf("\n\n No Memory2!");
>        >exit(1);
>    }
>
>    temp3 = (int *)calloc(col,sizeof(int));
>    if(temp3 == NULL){
>        >printf("\n\n No Memory3!");
>        >exit(1);
>    }
>
>    temp4 = (int *)calloc(col,sizeof(int));
>    if(temp4 == NULL){
>        printf("\n\n No Memory4!");
>        exit(1);
>    }
>
>    int *cvector;
>    cvector = (int *)calloc(col,sizeof(int));
>    if(cvector == NULL){
>        printf("\n\n No Memory5!");
>        exit(1);
>    }
>
>    tam=ntam;
>
>    int **matrix;
>    matrix=(int**)calloc(row,sizeof(int*));
>    if(matrix == NULL){
>        printf("\n\n No Memory6!");
>        exit(1);
>    }
>
>    temp=(int*)calloc(tam,sizeof(int));
>    if(temp == NULL){
>        printf("\n\n No Memory7!");
>        exit(1);
>    }
>
>    iia = (int *)calloc(row-1,sizeof(int));
>    if(iia == NULL){
>        printf("\n\n No Memory8!");
>        exit(1);
>    }
>
>    iib = (int *)calloc(row-1,sizeof(int));
>    if(iib == NULL){
>        printf("\n\n No Memory9!");
>        exit(1);
>    }
> ############################################
> In output !
>> No Memory5!
>> [marcelo at cocada01 home] $
>
> What's wrong?
> Thanks!
>
> --
> Marcelo Damasceno de Melo
> Graduando em Ciência da Computação
> Departamento de Tecnologia da Informação - TCI
> Universidade Federal de Alagoas - UFAL
> Maceió - Alagoas - Brasil
> Projeto CoCADa - Construção do Conhecimento por Agrupamento de dados
> +55 82 8801-2119
>
> 	[[alternative HTML version deleted]]
>
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595


More information about the R-devel mailing list