From ripley at stats.ox.ac.uk  Mon Oct 24 10:33:14 2005
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Mon, 24 Oct 2005 09:33:14 +0100 (BST)
Subject: [Rd] R_MakeExternalPtr
In-Reply-To: <F05B338A1B21D9119FFF0004761A511301ECD6@MXS>
References: <F05B338A1B21D9119FFF0004761A511301ECD6@MXS>
Message-ID: <Pine.LNX.4.61.0510240920250.8516@gannet.stats>

This is one of those undocumented things (AFAIK): LENGTH applies only to
'vectors'.  So don't use t and definitely don't set it for others.  The 
macro expands to

#define LENGTH(x)       (((VECSEXP) (x))->vecsxp.length)

What is a vector here?  Something which gets actually allocated by 
allocVector() AFAICS, that is an atomic or generic vector or an 
expression.  OTOH, an EXTPTRSXP is stored in a cons cell.

There is also a function length(), which is safer.

On Mon, 24 Oct 2005, TB wrote:

> Hi,
>
> I'm using R_MakeExternalPtr() to store handles to (COM) objects in a SEXP.
>
> The code basically is
>
> 	sexp = R_MakeExternalPtr(handle,R_NilValue,R_NilValue);
> 	R_RegisterCFinalizerEx(...);
>
> After creating the sexp, LENGTH(sexp) returns some quite large integer
> value. It seems like an "unitialized" value.
>
> Can I safely assume, that an SEXP of type EXTPTRSXP can only contain a
> single pointer value

It stores a pointer plus other things including a tag.

> and never represent a vector (an array) of pointers? Or
> should I maybe explicitly add something like
>
> 	SETLENGTH(sexp,1);
>
> Best wishes from sunny Austria,
>
> Thomas Baier
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>

-- 
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



[Rd] R_MakeExternalPtr
Prof Brian Ripley