How to point to same memory

Posts   
 
    
BlueCell avatar
BlueCell
User
Posts: 83
Joined: 12-Jul-2004
# Posted on: 11-Feb-2005 20:27:15   

Imagine I have this: Dim a as integer = 3 Dim b as integer = a a = 6

The result: a = 6; b = 3

How to get this result: a = 6; b = 6

As in... when changing a, I would like b to change aswell.

Thnx,

Michel vd Berg

Devildog74
User
Posts: 719
Joined: 04-Feb-2004
# Posted on: 11-Feb-2005 21:20:14   

        [STAThread]
        static unsafe void Main(string[] args)
        {

            int a = 6;
            int *x = &a;
            

        
            Console.WriteLine("a=" + a);
            Console.WriteLine("x=" + *x);
        
            a = 3;
        
            Console.WriteLine(*x);
            Console.ReadLine();
        }

youll need to allow unsafe code compiliation, and I am not sure if the code above is considered boxing and un boxing, but I think it fits the bill

wow, this is the first code I have written all week.

Devildog74
User
Posts: 719
Joined: 04-Feb-2004
# Posted on: 14-Feb-2005 21:43:44   

So since this code is a pointer to memory on the stack would this still be boxing? Isnt boxing / unboxing the process of converting value types to reference types and transfering data from the stack to the heap and vice versa?

Anyone? Frans?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39801
Joined: 17-Aug-2003
# Posted on: 14-Feb-2005 22:13:24   

Devildog74 wrote:

So since this code is a pointer to memory on the stack would this still be boxing? Isnt boxing / unboxing the process of converting value types to reference types and transfering data from the stack to the heap and vice versa? Anyone? Frans?

Correct, boxing is making value types (which are structs) being allocated inside an object type in the heap.

I'm not sure if the CLR will map unsafe pointer data in the managed heap or that it allocates that data on the normal heap. (I've always found those heaps pretty weird. On unix there is no heap, just memory, which keeps things simple)

Frans Bouma | Lead developer LLBLGen Pro
Devildog74
User
Posts: 719
Joined: 04-Feb-2004
# Posted on: 14-Feb-2005 22:45:26   

Cool, thanks. My wife is a Solaris 8 admin and she talks crazy unix things sometimes. Ill have to get her take on memory in the solaris world.

cmartinbot
User
Posts: 147
Joined: 08-Jan-2004
# Posted on: 15-Feb-2005 01:02:07   

BlueCell wrote:

Imagine I have this: Dim a as integer = 3 Dim b as integer = a a = 6

The result: a = 6; b = 3

How to get this result: a = 6; b = 6

As in... when changing a, I would like b to change aswell.

Thnx,

Michel vd Berg

The only way to do that in managed code is to wrap the integers (value types) into reference types.

C# example


public class MyClass
{
    public static void Main()
    {
        W a = new W(3);
        W b = a;        
        a.a = 6;
        
        Console.WriteLine( "a = {0}\nb = {1}", a.a, b.a );
        
        Console.WriteLine( "Press enter to exit." );
        
        Console.ReadLine();
    }
    
    class W
    {
        public int a;
        
        public W( int a )
        {
            this.a = a;
        }
    }
}

Devildog74
User
Posts: 719
Joined: 04-Feb-2004
# Posted on: 15-Feb-2005 21:22:30   

At that point you will definately be boxing and unboxing, which is supposedly a performance hit because of the copying to and from memory, but atleast its managed and safe.

cmartinbot
User
Posts: 147
Joined: 08-Jan-2004
# Posted on: 15-Feb-2005 23:13:59   

Devildog74 wrote:

At that point you will definately be boxing and unboxing, which is supposedly a performance hit because of the copying to and from memory, but atleast its managed and safe.

There is no way around it I'm afraid. It's just how value (primitive) types work.