D41d8cd98f00b204e9800998ecf8427e

I am trying to work out how unmanged objects work in C# but for some reason it looks like my objects arn't properly initized. Could someone help me out here?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace Pointers {
    unsafe class Program {
        static unsafe void Main( string[ ] args ) {
            Unsafe unsafeObject = new Unsafe( 1, 2, 3, 4 );

            Console.WriteLine(
                "FirstLocation[ " + unsafeObject.firstLocation->ToString( ) + " ]\n" +
                "SecondLocation[ " + unsafeObject.secondLocation->ToString( ) + " ]"
            );

            Console.Read( );
        }
    }

    unsafe struct Unsafe {
        public Safe* firstLocation, secondLocation;

        public Unsafe( int fl_x, int fl_y, int sl_x, int sl_y ) {
            Safe ffl = new Safe( fl_x, fl_y ),
                 fsl = new Safe( sl_x, sl_y );

            this.firstLocation = &ffl;
            this.secondLocation = &fsl;
        }
    }

    struct Safe {
        public int x, y;

        public Safe( int x, int y ){
            this.x = x;
            this.y = y;
        }

        public override string ToString( ) {
            return this.x + "," + this.y;
        }
    }
}
FirstLocation[ 1,2 ]
SecondLocation[ 3,4 ]
FirstLocation[ 14,67169456 ]
SecondLocation[ 19593296,19593248 ]

Refactorings

No refactoring yet !

D41d8cd98f00b204e9800998ecf8427e

Andre Steenveld

May 8, 2008, May 08, 2008 09:00, permalink

No rating. Login to rate!

I have found the problem. It appears that pointers to objects that are declared in the scope of a function don't stay alive after the function has returned. This other than in javascript for example.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace Pointers {
    class Program {
        unsafe static void Main( string[ ] args ) {
            Unsafe unsafeObject = new Unsafe( 1, 2, 3, 4 );

            Console.WriteLine(
                "FirstLocation[ " + unsafeObject.firstLocation->ToString( ) + " ]\n" +
                "SecondLocation[ " + unsafeObject.secondLocation->ToString( ) + " ]"
            );
            
            Console.Read( );
        }
    }

    unsafe struct Unsafe {
        public Safe* firstLocation, secondLocation;

        private Safe sfLocation, ssLocation;

        public Unsafe( int fl_x, int fl_y, int sl_x, int sl_y) {
            this.sfLocation = new Safe( fl_x, fl_y );
            this.ssLocation = new Safe( sl_x, sl_y );

            fixed( Safe* fl = &this.sfLocation )
            fixed( Safe* sl = &this.ssLocation ) {
                this.firstLocation = fl;
                this.secondLocation = sl;
            }
        }
    }

    struct Safe {
        public int x, y;

        public Safe( int x, int y ){
            this.x = x;
            this.y = y;
        }

        public override string ToString( ) {
            return this.x + "," + this.y;
        }
    }
}

Your refactoring





Format Copy from initial code

or Cancel