[Rd] data is getting corrupted

Duncan Murdoch murdoch at stats.uwo.ca
Tue Nov 30 03:44:32 CET 2004


On Mon, 29 Nov 2004 09:27:42 -0800 (PST), "Jeff D. Hamann"
<jeff.hamann at forestinformatics.com> wrote:

>/* this function converts the sample list	*/
>/* from R into the internal structure		*/
>struct SAMPLE_RECORD *build_sample_from_sexp( SEXP sample )
>{

[ declarations deleted ]

>
>   char                    temp_sp_code[16];
>   struct SAMPLE_RECORD *s_ptr;
>   struct SPECIES_RECORD *sp_ptr;
>
>   s_ptr = (struct SAMPLE_RECORD *)calloc( 1, sizeof( struct SAMPLE_RECORD
>) );

This allocates a structure, initialized to all zeros.

>/*    s_ptr = (struct SAMPLE_RECORD *)Calloc( 1,  struct SAMPLE_RECORD ); */
>
>   /* *fill in the header info */
>   strcpy( s_ptr->forest,
>CHAR(STRING_ELT(get_list_element(sample,"forest"), 0)) ) ;

This copies the string element to the address s_ptr->forest points to,
which is address 0, since you didn't change it from the initial NULL.
I'm surprised you didn't get a bigger error than the one you saw.

[ more deletions ]

>I'm sure there's something I don't understand about the PROTECT/UNPROTECT
>sequence as this seems to work on smaller data.frames

Maybe you were just lucky that the overwriting at address 0 didn't
trash anything in those cases?

Duncan Murdoch



More information about the R-devel mailing list