[Rd] Memory address of character datatype

Tomas Kalibera tomas.kalibera at gmail.com
Thu Nov 2 10:08:05 CET 2017


If you were curious about the hidden details of the memory layout in R, 
the best reference is the source code. In your example, you are not 
getting to your string because there is one more pointer in the way, "x" 
is a vector of strings, each string is represented by a pointer.

At C level, there is an API for getting an address of the value, e.g. 
INTEGER(x) or CHAR(STRING_ELT(x)).
At R level, there is no such API.

You should never bypass these APIs.  The restrictions of the APIs allow 
us to change details of the memory layout between svn versions or even 
as the program executes (altrep), in order to save memory or improve 
performance. Also, it means that the layout can be slightly different 
between platforms, e.g. 32-bit vs 64-bit.

Unfortunately address(x) from pryr bypasses the APIs - you should never 
use address(x) in your programs and I wish address(x) did not exist. If 
you had a concrete problem at hand you wanted to solve with 
"address(x)", feel free to ask for a viable solution.

Best
Tomas




On 11/01/2017 07:37 PM, lille stor wrote:
> Hi,
>   
> To get the memory address of where the value of variable "x" (of datatype "numeric") is stored one does the following in R (in 32 bit):
>   
>        library(pryr)
>        x <- 1024
>        addr <- as.numeric(address(x)) + 24    # 24 is needed to jump the variable info and point to the data itself (i.e. 1024)
>   
> The question now is what is the value of the jump so that one can obtain the memory address of where the value of variable "x" (of datatype "character"):
>   
>
>        library(pryr)
>        x <- "abc"
>        addr <- as.numeric(address(x)) + ??    # what should be the value of the jump so that it points to the data of variable "x" (i.e. abc)?
>   
> Thank you in advance!
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



More information about the R-devel mailing list