[R] Passing string arguments to C code, call to .C, R memory allocation for character vectors
Harold PETITHOMME
harold.petithomme at meteo.fr
Fri Jan 25 10:29:50 CET 2008
Hello R-ill world!
My problem : I obtain a segmentation fault when passing a character
argument to a C function, compiled in a shared object and loaded by
dyn.load.
1. As manuals (and overall "Writing R extensions ") don't seem to
mention it or I failed to find the info, could someone explain to me
what is exactly happening when R allocates memory for character vectors?
Manuals precise that they have to be handled as char ** pointers in C,
but this does not tell much about effective allocation although it is
said to be 255 long character pointers for Fortran code.
2. As I understand, R allocates vectors and arrays in Fortran mode, that
is all dimensions in a single block. This means passing a char * pointer
to C, which is not true seemingly. Say this pointer is s, is my i-th
character element s[i] in C or (*s)+LEN*i (LEN=255 as I suppose) or
something else?
3. My simple R & C code gives me a segmentation fault. Trying to write
an R character vector in the C function, the fault comes or not,
depending on the vector dimension or on the element length. I suspect a
memory protection from R or a faulty adressing in my C function (of
course, I do as I understand from manuals). Can anybody track the
error(s)? Note that the 2 tested R versions give different error
messages (R-2.0.0 and R-2.3.0 on 2 Linux machines, one : Linux host1
2.4.21-32.ELsmp, other : Linux host2 2.6.9-34.ELsmp)
Here is the code :
R programme :
n = 50
cat("R call to litchar\n")
dyn.load("lit.so")
ret = .C("litchar",noms=character(n),as.integer(n))
---
C code :
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void litchar(char **s,int *n)
{
int i, j, c;
for (i=0;i<*n;i++) {
sprintf(s[i],"%6d ",i); /* writes 'i' in s' i-th element... */
c = 'a' + (i % 26); /* c is the i-th alphabet letter (set by
ASCII code) */
for (j=7 ; j<100 ; j++) s[i][j] = c; /* ... then add the c
sequence of aaaaa, bbbbb, etc. */
s[i][j] = 0; /* C strings terminator */
fprintf(stderr,"s[%6d] : \"%s\" (%p)\n",i,s[i], s[i]); /* writes
out i, s i-th element and its adress */
}
}
---
Last one : s[i] adresses seem to be not contiguous. How could it be?
Isn't there a corruption in memory allocation tables?
Many thanks in advance for those who help.
Best regards,
Harold.
More information about the R-help
mailing list