[R] Error messages and C
tlumley at u.washington.edu
Sat Aug 21 01:39:31 CEST 2004
On Fri, 20 Aug 2004, Ross Boylan wrote:
> The weak references cleaned up are R objects, and my stuff is mostly
> non-R objects. I see two possible ways to get this to work:
> 1) Create some kind of dummy R object with a finalizer that cleans up my
> C++ objects.
That's the approach used in that web page.
> > If you think of p as a vector of strings (ie char *p) then it
> > corresponds to whatever strings you passed in from R. You can modify
> > these in place, possibly making them shorter. So if you need 80 characters
> > of error message, pass in a string of length at least 80.
> If space permits, is this just an ordinary, C-style null terminated
> string? Or does R have some notion of the string length which is
> unaffected by what I stuff in it?
For an R object there is a notion of length, but the things passed to .C
are copied back with strcpy and the R length is set to the strlen of the C
string. So they are just null-terminated C strings.
This would cause problems with DUP=FALSE, which is why you aren't allowed
to pass strings with DUP=FALSE.
> > If you want to allocate more memory you need to do this through R (either
> > R_alloc or the Rinternals functions). Using malloc() and stuffing the
> > result in, say, p, will cause a memory leak since you won't be able to
> > find it and free it.
> So if I overwrite p, for example, the R garbage collector will know
> to clean up the old string that was pointed to from that location?
Yes. In fact the strings are just temporary versions that are copied
back to new R objects and destroyed when you return.
More information about the R-help