[R] STRING_PTR_RO compared to STRING_ELT

Tim Taylor t|m@t@y|or @end|ng |rom h|ddene|eph@nt@@co@uk
Mon Oct 5 18:05:54 CEST 2026


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.



More information about the R-help mailing list