Exercises

1. Why is tracemem(1:10) not useful?

Because it tries to trace a value that is not bound to a name

2. Why are there two copies?
x <- c(1L, 2L, 3L)
tracemem(x)
#> [1] "<0x560e545d9328>"
x[[3]] <- 4
#> tracemem[0x560e545d9328 -> 0x560e545f63c8]: eval eval withVisible withCallingHandlers eval eval with_handlers doWithOneRestart withOneRestart withRestartList doWithOneRestart withOneRestart withRestartList doWithOneRestart withOneRestart withRestartList withRestarts <Anonymous> evaluate in_dir in_input_dir eng_r block_exec call_block process_group withCallingHandlers <Anonymous> process_file <Anonymous> <Anonymous> render_cur_session <Anonymous> 
#> tracemem[0x560e545f63c8 -> 0x560e53295868]: eval eval withVisible withCallingHandlers eval eval with_handlers doWithOneRestart withOneRestart withRestartList doWithOneRestart withOneRestart withRestartList doWithOneRestart withOneRestart withRestartList withRestarts <Anonymous> evaluate in_dir in_input_dir eng_r block_exec call_block process_group withCallingHandlers <Anonymous> process_file <Anonymous> <Anonymous> render_cur_session <Anonymous>

Because we convert an integer vector (using 1L, etc.) to a double vector (using just 4)-

3. What is the relationships among these objects?
a <- 1:10      
b <- list(a, a)
c <- list(b, a, 1:10) # 

a <- obj 1
b <- obj 1, obj 1
c <- b(obj 1, obj 1), obj 1, 1:10

ref(c)
#> █ [1:0x560e53c68f48] <list> 
#> ├─█ [2:0x560e52fbbb88] <list> 
#> │ ├─[3:0x560e5396c908] <int> 
#> │ └─[3:0x560e5396c908] 
#> ├─[3:0x560e5396c908] 
#> └─[4:0x560e53ac7130] <int>
4. What happens here?
x <- list(1:10)
x[[2]] <- x
  • x is a list
  • x[[2]] <- x creates a new list, which in turn contains a reference to the original list
  • x is no longer bound to list(1:10)
ref(x)
#> █ [1:0x560e52916748] <list> 
#> ├─[2:0x560e5318aae8] <int> 
#> └─█ [3:0x560e5449c4f8] <list> 
#>   └─[2:0x560e5318aae8]