# Is Possible to Know if a Variable Changed in Memory?

**URL:** <https://discourse.julialang.org/t/is-possible-to-know-if-a-variable-changed-in-memory/91210>\
**Category:** General Usage\
**Tags:** memory, memory-allocation\
**Created:** [December 4, 2022, 6:57am UTC](https://discourse.julialang.org/t/is-possible-to-know-if-a-variable-changed-in-memory/91210 "2022-12-04T06:57:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cdelv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cdelv/32/44851_2.png) [@cdelv](https://discourse.julialang.org/u/cdelv)\
**Post date:** [December 4, 2022, 6:57am UTC](https://discourse.julialang.org/t/is-possible-to-know-if-a-variable-changed-in-memory/91210/1 "2022-12-04T06:57:23Z")

</div>

This question is just for curiosity, but is it possible to know if a variables changed? For example

```julia
v = [1,2,3]

changed?(v)
false

v[1]=2
changed?(v)
true

```

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [December 4, 2022, 10:31am UTC](https://discourse.julialang.org/t/is-possible-to-know-if-a-variable-changed-in-memory/91210/2 "2022-12-04T10:31:51Z")

</div>

No, whether the memory backing a variable is changed is not a property that is tracked per se. Though you could define such a thing for your own type, it’s not tracked on the language level.

Further, variables/bindings to not have a defined location in memory - they may not live in memory at all, if they are elided during compilation or removed entirely due to constant folding. At most, they refer to some object - where that object is in memory is not defined. For example:

```julia
julia> function foo(x)
           a = 0
           for i in 1:x
               a += i
           end
           a
       end
foo (generic function with 1 method)

julia> @code_llvm debuginfo=:none foo(10)
define i64 @julia_foo_356(i64 signext %0) #0 {
[...] # function setup

L17.preheader: ; preds = %top
  %4 = shl nuw i64 %., 1
  %5 = add nsw i64 %., -1
  %6 = zext i64 %5 to i65
  %7 = add nsw i64 %., -2
  %8 = zext i64 %7 to i65
  %9 = mul i65 %6, %8
  %10 = lshr i65 %9, 1
  %11 = trunc i65 %10 to i64
  %12 = add i64 %4, %11
  %13 = add i64 %12, -1
  br label %L32

L32: ; preds = %L17.preheader, %top
  %value_phi11 = phi i64 [0, %top], [%13, %L17.preheader]
  ret i64 %value_phi11
}

```

So the compiler removed the loop and put a direct computation of the result there. If we then wrap this function in another one, hardcoding the argument:

```julia
julia> g() = foo(10)
g (generic function with 1 method)

julia> @code_llvm debuginfo=:none g()
define i64 @julia_g_411() #0 {
top:
[...] # function setup 
  ret i64 55
}

```

We can see that it removed all of the computation and just plainly gives you the result back.

---

<div class="post-metadata">

**Author:** ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)\
**Post date:** [December 4, 2022, 10:47am UTC](https://discourse.julialang.org/t/is-possible-to-know-if-a-variable-changed-in-memory/91210/3 "2022-12-04T10:47:32Z")

</div>

> [@cdelv](#):
>
> This question is just for curiosity, but is it possible to know if a variables changed? For example
> 
> ``

It is not possible in julia as such, but it is possible with a debugger such as gdb (with the `watch` command).

---

<div class="post-metadata">

**Author:** ![sairus7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sairus7/32/10816_2.png) [@sairus7](https://discourse.julialang.org/u/sairus7)\
**Post date:** [May 10, 2023, 8:34am UTC](https://discourse.julialang.org/t/is-possible-to-know-if-a-variable-changed-in-memory/91210/4 "2023-05-10T08:34:00Z")

</div>

Maybe you are looking for [Observables.jl](https://juliagizmos.github.io/Observables.jl/stable/)?

---

<div class="post-metadata">

**Author:** ![Nikos\_Gianniotis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nikos_gianniotis/32/11487_2.png) [@Nikos\_Gianniotis](https://discourse.julialang.org/u/Nikos_Gianniotis)\
**Post date:** [May 25, 2023, 6:53pm UTC](https://discourse.julialang.org/t/is-possible-to-know-if-a-variable-changed-in-memory/91210/5 "2023-05-25T18:53:45Z")

</div>

Thanks, I was looking for it!  
Also: related to Observables.jl there is a package called [Reactive.jl](https://github.com/JuliaGizmos/Reactive.jl).

---

<div class="post-metadata">

**Author:** ![sairus7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sairus7/32/10816_2.png) [@sairus7](https://discourse.julialang.org/u/sairus7)\
**Post date:** [May 25, 2023, 10:49pm UTC](https://discourse.julialang.org/t/is-possible-to-know-if-a-variable-changed-in-memory/91210/6 "2023-05-25T22:49:57Z")

</div>

There are also [Rocket.jl](https://github.com/biaslab/Rocket.jl), [Signals.jl](https://github.com/TsurHerman/Signals.jl)
