Memory address of character datatype

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Memory address of character datatype

lille stor
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!

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Memory address of character datatype

Tomas Kalibera
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!
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Memory address of character datatype

Peter Dalgaard-2
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.

-pd

> On 2 Nov 2017, at 10:08 , Tomas Kalibera <[hidden email]> 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!
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>
> ______________________________________________
> [hidden email] 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: [hidden email]  Priv: [hidden email]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel