[R] STRING_PTR_RO compared to STRING_ELT
Iris Simmons
|kw@|mmo @end|ng |rom gm@||@com
Mon Oct 5 18:19:43 CEST 2026
Sorry, I made a mistake in regards to the comment about standard vs
alternate vectors. It seems as though it does work, it just may result in
an allocation when called on an alternate vector (but interestingly cannot
trigger a garbage collection).
Function STRING_PTR_RO
https://github.com/wch/r-source/blob/d75f6a2224865f9ac0bee065de99af817a3a65e6/src/main/memory.c#L4277
which calls DATAPTR_RO
https://github.com/wch/r-source/blob/d75f6a2224865f9ac0bee065de99af817a3a65e6/src/include/Defn.h#L434
https://github.com/wch/r-source/blob/d75f6a2224865f9ac0bee065de99af817a3a65e6/src/include/Rinlinedfuns.h#L148
which may call ALTVEC_DATAPTR_RO
https://github.com/wch/r-source/blob/d75f6a2224865f9ac0bee065de99af817a3a65e6/src/main/altrep.c#L379
which calls ALTVEC_DATAPTR_EX
https://github.com/wch/r-source/blob/d75f6a2224865f9ac0bee065de99af817a3a65e6/src/main/altrep.c#L352
On Mon, Oct 5, 2026, 12:06 Tim Taylor <tim.taylor using hiddenelephants.co.uk>
wrote:
> Many thanks Iris
>
> That's very helpful.
>
> Tim
>
> On Mon, 5 Oct 2026, at 5:01 PM, Iris Simmons wrote:
> > STRING_PTR_RO(x)[i] will not check bounds and will not check that the
> > element is not NULL and not a free node. STRING_PTR_RO only works on
> > standard vectors, and will not work on alternate vector
> > representations. Also, it will return (const SEXP *) (void *) 1 when
> > the character vector is of length 0, or may return a pointer to a
> > random place in memory. In general, I would suggest
> >
> > const SEXP *y = NULL;
> > if (Rf_xlength(x)) y = STRING_PTR_RO(x);
> >
> > or something similar in order to avoid anything weird involving 0 length
> access.
> >
> > I do not know of any other differences to note.
> >
> > On Mon, Oct 5, 2026, 11:36 Tim Taylor <tim.taylor using hiddenelephants.co.uk>
> wrote:
> >> Apart from different ergonomics is there anything worth being aware of
> when using STRING_PTR_RO versus STRING_ELT for getting CHARSXP?
> >>
> >> Tim
> >>
> >> ______________________________________________
> >> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> >> https://stat.ethz.ch/mailman/listinfo/r-help
> >> PLEASE do read the posting guide
> https://www.R-project.org/posting-guide.html
> >> and provide commented, minimal, self-contained, reproducible code.
>
[[alternative HTML version deleted]]
More information about the R-help
mailing list