[Rd] Memory address of character datatype

peter dalgaard pdalgd at gmail.com
Thu Nov 2 11:44:07 CET 2017

I'm not really disagreeing with this, but is not the point of pryr to let you investigate internals from the R level? 

Building code that relies on pryr returning things with specific properties is very likely doubleplusunrecommended by pryr's author as well.

In that spirit, I suppose that you could reasonably wish for features that would let you peek at memory locations and follow pointers around, etc. As long as you don't poke() anything into memory, you are not likely to break anything. (Hmm, unless you try printing non-objects and suchlike...) Of course users should be aware that any change to R internals may invalidate previously working code (e.g., by changing the "24" in the OP's example).  I don't see any such functionality in pryr though.


> On 2 Nov 2017, at 10:08 , Tomas Kalibera <tomas.kalibera at gmail.com> wrote:
> 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
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com

More information about the R-devel mailing list