2

Why with this code:

unsafe
    {
        for (int i = 0; i < 10; i++)
        {
            Double w = new Double();
            Console.WriteLine((IntPtr)(&w));
        }
    }

i'm always getting the same number? How to create new variables in a loop ? With new addresses ?

The reason, why this is a problem for me is that I need to generate a random double number and then I'm using a pointer (which references to that double) in two objects. In one of the object I'm changing this value and I want it to change in that other object too :)

Benas Radzevicius
  • 279
  • 1
  • 3
  • 17
  • 3
    "`i`" isn't doing anything except incrementing; and you're not doing *anything* with "`w`" except writing (possibly uninitialized) garbage. – Michael Dautermann Sep 28 '13 at 23:00
  • I'm guessing it's some compiler optimization. The compiler can probably tell that you're not doing anything with `w`, so it doesn't create a new instance everytime. – Rohan Sep 28 '13 at 23:02
  • 2
    @MichaelDautermann: The second half of your analysis is incorrect. Why would the address be garbage? – Eric Lippert Sep 28 '13 at 23:03
  • @EricLipper We don't know what value "`w`" is set to. Maybe zero? Maybe 23434124231234. Maybe `0xDEADBEEF`. – Michael Dautermann Sep 28 '13 at 23:04
  • 3
    @MichaelDautermann he's looking for the memory address of `w`, not the value of `w` – Dave Zych Sep 28 '13 at 23:05
  • By the way, what are you trying to **achieve** with this code? – Sergey Vyacheslavovich Brunov Sep 28 '13 at 23:15
  • 1
    @MichaelDautermann `Double` is a value type, so sure do we know what value `w` is set to: `0.0`. And even if he was using `w` in whatever way; it would still run out of scope and its address could be reused in the next iteration. – poke Sep 28 '13 at 23:30
  • @MichaelDautermann: `new Double()`, `default(double)` and `0.0` are all ways to write the same thing. – Eric Lippert Sep 29 '13 at 01:09

2 Answers2

14

Logically, that's a new variable every time through the loop. But I hope that you would not expect a new variable to actually be allocated off the stack every time! What if the loop runs a million times? The compiler knows that it can re-use the storage, and it does.

If you want ten different addresses, make an array with ten elements, fix it in place, and take the address of each element.

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
  • So, if he declared the variable outside the loop and instantiated it inside the loop, would he be getting a new address everytime (since the compiler isn't creating more space on the stack)? – Rohan Sep 28 '13 at 23:08
  • Yes, "But I hope that you would not expect a new variable to actually be allocated off the stack every time!" this is exactly what I need to do, because I need to generate a random double number and then I'm using a pointer (which references to that double) in two classes. In one of the class I'm changing this value and I want it to change in that other class too :) – Benas Radzevicius Sep 28 '13 at 23:15
  • 3
    @BenasRadzevicius it sounds like you might be using C# wrong. – Dave Zych Sep 28 '13 at 23:17
2

It seems you want to share the reference to the value of the value type. So, it can be done by wrapping the value type into the reference type:

class Ref<T>
{
    public T Value { get; set; }
}

Please also see the related question: C# - Good and flexible way to pass value types by reference?.

Community
  • 1
  • 1