[R] R_alloc with structures with "flexible array members"

Jeffrey Horner jeff.horner at vanderbilt.edu
Wed Mar 5 22:38:29 CET 2008


Ramon Diaz-Uriarte wrote on 03/05/2008 03:00 PM:
> Dear Prof. Ripley,
> 
> Yes, of course! You are right. What a silly mistake on my part! I was
> using a standalone program for development of functions, debugging,
> etc, of what is part of a package.

Aha! The lesson I take away from this then is question everything, 
assume nothing.

Jeff

> 
> Thanks,
> 
> R.
> 
> 
> 
> On Wed, Mar 5, 2008 at 8:45 PM, Prof Brian Ripley <ripley at stats.ox.ac.uk> wrote:
>> On Wed, 5 Mar 2008, Ramon Diaz-Uriarte wrote:
>>
>>  > Dear Jeff,
>>  >
>>  > Thanks for the suggestion. However, something is still not working.
>>  > This is a simple example:
>>  >
>>  > ***************************  start C ************
>>  > #include <R.h>
>>  >
>>  > struct Sequence {
>>  >  int len;
>>  >  unsigned int state_count[];
>>  > };
>>  >
>>  >
>>  > int main(void) {
>>  >
>>  >  struct Sequence *A;
>>  >  int n = 4;
>>  >
>>  >  // First line segfaults. Second doesn't
>>  >    A = (struct Sequence *) R_alloc(1,  sizeof(struct Sequence) + n *
>>  > sizeof(unsigned int));
>>  >  //  A = malloc(sizeof(struct Sequence) + n * sizeof(unsigned int));
>>  >
>>  >  return(0);
>>  > }
>>  >
>>  > ***********  end C  **********
>>  >
>>  >
>>  > I then do
>>  > gcc -std=gnu99 -Wall -I/usr/share/R/include -I/usr/share/R/include
>>  > -L/usr/lib/R/lib -lR ex7.c
>>  >
>>  > and the ./a.out segfaults when I use R_alloc (not with malloc).
>>
>>  You can't use R_alloc in a standalone program without initializing R,
>>  which has not been done here.
>>
>>  You said 'in a package', but this is not in a package.
>>
>>
>>
>>  >
>>  >
>>  > Best,
>>  >
>>  > R.
>>  >
>>  > On Wed, Mar 5, 2008 at 5:23 PM, Jeffrey Horner
>>  > <jeff.horner at vanderbilt.edu> wrote:
>>  >> Ramon Diaz-Uriarte wrote on 03/05/2008 04:25 AM:
>>  >>
>>  >>
>>  >>> Dear All,
>>  >> >
>>  >> > In a package, I want to use some C code where I am using a structure
>>  >> > (as the basic element of a linked list) with flexible array members.
>>  >> > Basically, this is a structure where the last component is an
>>  >> > incomplete array type  (e.g., Harbison & Steel, "C, a reference
>>  >> > manual, 5th ed.", p. 159) such as:
>>  >> >
>>  >> > struct Sequence {
>>  >> >   struct Sequence *next;
>>  >> >   int len;
>>  >> >   unsigned int state_count[];
>>  >> > };
>>  >> >
>>  >> >
>>  >> > To create one such sequence, I allocate storage (following Harbison
>>  >> > and Steel) in a C program as follows:
>>  >> >
>>  >> > struct Sequence *A;
>>  >> > int n = 4;
>>  >> > A = malloc( sizeof(struct Sequence) + n * sizeof(unsigned int));
>>  >> >
>>  >> >
>>  >> > If I understand correctly, however, it would be better to use R_alloc
>>  >> > instead of malloc (memory automagically freed on exit and error;
>>  >> > error-checking). But I do not know how to make the call to R_alloc
>>  >> > here, since R_alloc allocates n units of size bytes each.
>>  >> >
>>  >> >
>>  >> > I've tried, without success, the following two:
>>  >> >
>>  >> > int to_add_for_R_alloc =
>>  >> >     (int) ceil((float) sizeof(struct sequence) / sizeof(unsigned int));
>>  >> >
>>  >> >   A = (struct sequence *) R_alloc(to_add_for_R_alloc + n,
>>  >> > sizeof(unsigned int));
>>  >> >
>>  >> > or even a brute force attempt as:
>>  >> >
>>  >> >  A = (struct sequence *) R_alloc( 100,  sizeof(struct sequence));
>>  >> >
>>  >> >
>>  >> > but both result in segmentation faults.
>>  >> >
>>  >> >
>>  >> > Should I just keep using malloc (and free at end)?
>>  >>
>>  >>  Hi Ramon,
>>  >>
>>  >>  You should be able to use R_alloc without seg faults, so there's
>>  >>  something wrong with your code somewhere. R_alloc multiplies its
>>  >>  arguments together to come up with the total number of bytes to allocate
>>  >>  then it allocates a raw vector and returns the data portion.
>>  >>
>>  >>  So you can just treat R_alloc similarly to malloc by calling
>>  >>  R_alloc(1,sizeof(struct Sequence) + n * sizeof(unsigned int)).
>>  >>
>>  >>  Best,
>>  >>
>>  >>  Jeff
>>  >>  --
>>  >>  http://biostat.mc.vanderbilt.edu/JeffreyHorner
>>  >>
>>  >
>>  >
>>  >
>>  > --
>>  > Ramon Diaz-Uriarte
>>  > Statistical Computing Team
>>  > Structural Biology and Biocomputing Programme
>>  > Spanish National Cancer Centre (CNIO)
>>  > http://ligarto.org/rdiaz
>>  >
>>  > ______________________________________________
>>  > R-help at r-project.org mailing list
>>  > https://stat.ethz.ch/mailman/listinfo/r-help
>>  > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>  > and provide commented, minimal, self-contained, reproducible code.
>>  >
>>
>>  --
>>  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-help mailing list