[Rd] Array changing address unexpectedly

David Winsemius dwinsemius at comcast.net
Sun Nov 12 18:02:23 CET 2017


> On Nov 12, 2017, at 8:47 AM, lille stor <lille.stor at gmx.com> wrote:
> 
> Hi,
> 
> Given the following R code:
> 
>     library(pryr)
> 
>     data <- array(dim = c(5))
> 
>     for(x in 1:5)
>     {
>          data[x] <- as.integer(x * 2)
>     }
> 
>     add = address(data) # save address of "data"
> 
>     for(x in 1:5)
>     {
>          data[x] <- as.integer(0)
>     }
> 
>     if (add == address(data))
>     {
>        print("Address did not change")
>     }
>     else
>     {
>        print("Address changed")
>     }
> 
> If one runs this code, message "Address changed" is printed. However, if one comments line "data[x] <- as.integer(0)" the address of "data" does not change and message "Address did not change" is printed instead. Why? The datatype of the array should not change with this line and hence no need for R to convert the array to a different type (and have the array's address changing in the process).

I'm guessing you didn't take note of the error message:

>     else
Error: unexpected 'else' in "    else"

It's always good practice to investigate errors. The else function needs to come immediately after the "{".

Here's a more complete test of what I take to be your question:

library(pryr)

data <- array(dim = c(5))
add = address(data)
    for(x in 1:5)
    {
         data[x] <- as.integer(x * 2)
    }
 if (add == address(data))
    {
       print("Address did not change")
    } else {
       print("Address changed")
    }


data <- array(dim = c(5))   # reset
add = address(data)
    for(x in 1:5)
    {
         data[x] <- as.integer(0)
    }

    if (add == address(data))
    {
       print("Address did not change")
    } else {
       print("Address changed")
    }

# changes in both situations.


> 
> Thank you!
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

David Winsemius
Alameda, CA, USA

'Any technology distinguishable from magic is insufficiently advanced.'   -Gehm's Corollary to Clarke's Third Law



More information about the R-devel mailing list