[Rd] UPDATE: Fwd: Memory...
Hin-Tak Leung
hin-tak.leung at cimr.cam.ac.uk
Wed Aug 23 23:10:22 CEST 2006
I think it needs to be strlen()+1, for the terminating null, or you'll
just be pray for your luck that your compiler actually
null-pad allocations to the nearest 4-byte or whatever and
so your program doesn't crash as it should later when you
try to read argV elsewhere. As for the other question about getopt,
you need to do this, somewhere:
...
extern int optind;
...
optind = 1; // resetting getopt
...
FWIW, your initial question ("repeated call doesn't work") isn't memory
related, nor R-related at all, but about not reading the man page of
getopt() and understand how it works - you should have included *the
rest* of the code, rather than just the bit you think is wrong,
because as it turn out, the bits you included is not your primary
problem.
Sender wrote:
> UPDATE: Okay I've narrowed the problem down. Within my C routine I use the
> getopt() method from <getopt.h>. For some reason when I recall this routine
> via my .Call, getopt() hasn't "refreshed" that is it still points to the end
> of my parsed command line arguments. Is R keeping this function alive in
> memory? Is that why its retaining its previous values? How can I get R to
> terminate this routine and reless its memory?
>
> Thanks again for any suggetions!
>
> Original Email ---
>
> Hello:
>
> I have a general question about memory allocation. I'm having a problem
> with an R function -- call it myRfunction -- that calls a C routine using
> .Call. For some reason it works the first time I call it, but for every
> subsequent time it doesn't. I'm thinking this has to be a memory allocation
> issue. If I quit R and then reload my package containing myRfunction, again
> it works only the first time its called.
>
> Here's how I allocate memory with in the C routine.
>
> for( i=0; i<argC; i++){
> argV[i] = R_alloc(strlen(CHAR_STRING_ELT(argv,i))), sizeof(char)) :
> strcpy( argV[i], CHAR(STRING_ELT(argv,i))) ;
> }
>
> Can anyone give me some suggestions about how to debug this issue to see if
> its indeed a memory problem? Could it be anything else?
>
> Should I look into vmax, vmaxset etc ?
>
> Thanks to everyone in advance for your help!
>
> Greg
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
More information about the R-devel
mailing list