Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Oject memory adress.

Oject memory adress.

Scheduled Pinned Locked Moved C#
csharpperformance
10 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GriffonRL
    wrote on last edited by
    #1

    Hello, I'm looking for a way to get the memory adress of a C# object. Any idea is welcome :). Thanks, R. LOPES Just programmer.

    L 2 Replies Last reply
    0
    • G GriffonRL

      Hello, I'm looking for a way to get the memory adress of a C# object. Any idea is welcome :). Thanks, R. LOPES Just programmer.

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      This is specifically hidden to the user as object address change continuously due to the GC. If you debug a program with CorDbg, these addresses will be shown, but like said, unlike C/C++ they are likely to move during each garbage collection. Cheers :)

      leppie::AllocCPArticle(Generic DFA State Machine for .NET);

      G 1 Reply Last reply
      0
      • L leppie

        This is specifically hidden to the user as object address change continuously due to the GC. If you debug a program with CorDbg, these addresses will be shown, but like said, unlike C/C++ they are likely to move during each garbage collection. Cheers :)

        leppie::AllocCPArticle(Generic DFA State Machine for .NET);

        G Offline
        G Offline
        GriffonRL
        wrote on last edited by
        #3

        Hi Leppie, I was expecting something like that. It makes totally sense. I'm now sure. Thanks, R. LOPES Just programmer.

        L 2 Replies Last reply
        0
        • G GriffonRL

          Hi Leppie, I was expecting something like that. It makes totally sense. I'm now sure. Thanks, R. LOPES Just programmer.

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          You wont believe it, but I'm stuck with this issue as well (building a VM emulating the CLR VM on top of .NET). If you are intereseted look at the ld.XXXa IL instructions. There seems to be no highlevel functions for this. Maybe something like this would work (no it wont dammit). Maybe... load an address on stack and then return that as IntPtr, but you have to carefull... As you can see I'm bit lost too! What I do know (think rather) is that be default GetHashCode() returns the signed addr of a non Value /String object. I could be wrong.

          leppie::AllocCPArticle(Generic DFA State Machine for .NET);

          G 1 Reply Last reply
          0
          • L leppie

            You wont believe it, but I'm stuck with this issue as well (building a VM emulating the CLR VM on top of .NET). If you are intereseted look at the ld.XXXa IL instructions. There seems to be no highlevel functions for this. Maybe something like this would work (no it wont dammit). Maybe... load an address on stack and then return that as IntPtr, but you have to carefull... As you can see I'm bit lost too! What I do know (think rather) is that be default GetHashCode() returns the signed addr of a non Value /String object. I could be wrong.

            leppie::AllocCPArticle(Generic DFA State Machine for .NET);

            G Offline
            G Offline
            GriffonRL
            wrote on last edited by
            #5

            Hi Leppie, What is this crazy idea of yours to build a VM on top of the VM of .NET :) ? Nevertheless, thank you for sharing with me your researches. By the way, for your nBass project, the BASS library has just been updated to 1.8a. Cheers, R. LOPES Just programmer.

            L 2 Replies Last reply
            0
            • G GriffonRL

              Hi Leppie, What is this crazy idea of yours to build a VM on top of the VM of .NET :) ? Nevertheless, thank you for sharing with me your researches. By the way, for your nBass project, the BASS library has just been updated to 1.8a. Cheers, R. LOPES Just programmer.

              L Offline
              L Offline
              leppie
              wrote on last edited by
              #6

              This does the job perhaps:

              .method private hidebysig static void*
              GetAddr(object& o) cil managed
              {
              .maxstack 1
              ldarg.0
              ret
              }

              You need to pass a ref object, so cast the 'o' to object. GriffonRL wrote: What is this crazy idea of yours to build a VM on top of the VM of .NET ? Just what you say: Crazy! But the ideas change all the time! The new idea is to have a Side by Side VM, with a different IL language (read more simplified). GriffonRL wrote: By the way, for your nBass project, the BASS library has just been updated to 1.8a. Thnx, I havent worked with it for ages... maybe when I'm bored... X|

              leppie::AllocCPArticle(Generic DFA State Machine for .NET);

              1 Reply Last reply
              0
              • G GriffonRL

                Hi Leppie, What is this crazy idea of yours to build a VM on top of the VM of .NET :) ? Nevertheless, thank you for sharing with me your researches. By the way, for your nBass project, the BASS library has just been updated to 1.8a. Cheers, R. LOPES Just programmer.

                L Offline
                L Offline
                leppie
                wrote on last edited by
                #7

                Actually from that previous IL method it appears that when passing any object by ref, the CLR creates a pointer that holds the object, still not what you are looking for though.

                leppie::AllocCPArticle(Generic DFA State Machine for .NET);

                1 Reply Last reply
                0
                • G GriffonRL

                  Hi Leppie, I was expecting something like that. It makes totally sense. I'm now sure. Thanks, R. LOPES Just programmer.

                  L Offline
                  L Offline
                  leppie
                  wrote on last edited by
                  #8

                  OK here is what works:

                  .method private hidebysig static void*
                  GetAddr(object& o) cil managed
                  {
                  .maxstack 1
                  ldarg.0 //loads address
                  conv.u //make sure its a M*
                  ret //return the M*
                  }

                  Output from Cordbg (after calling meth, but passing only c1, NOT ref c1): object c1 =(0x00c41910) void* h = 0x00C41910 I cant see a way to do this via C# though.

                  leppie::AllocCPArticle(Generic DFA State Machine for .NET);

                  1 Reply Last reply
                  0
                  • G GriffonRL

                    Hello, I'm looking for a way to get the memory adress of a C# object. Any idea is welcome :). Thanks, R. LOPES Just programmer.

                    L Offline
                    L Offline
                    leppie
                    wrote on last edited by
                    #9

                    Here is a full sample, see the comments:

                    .assembly extern mscorlib
                    {
                    .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )
                    .ver 1:0:3300:0
                    }
                    .assembly Testing
                    {
                    .hash algorithm 0x00008004
                    .ver 1:0:1272:38245
                    }
                    .module Testing.exe
                    .custom instance void [mscorlib]System.Security.UnverifiableCodeAttribute::.ctor() = ( 01 00 00 00 )
                    .imagebase 0x00400000
                    .subsystem 0x00000003
                    .file alignment 512
                    .corflags 0x00000001

                    .namespace Testing
                    {
                    .class private auto ansi beforefieldinit Class1
                    extends [mscorlib]System.Object
                    {
                    .method private hidebysig static void
                    Main(string[] args) cil managed
                    {
                    .entrypoint
                    .custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01 00 00 00 )
                    .maxstack 1
                    .locals ([0] object c1,
                    [1] void* h,
                    [2] int32 i)
                    newobj instance void Testing.Class1::.ctor()
                    stloc.0
                    ldloc.s c1 //ldloca will box ref type
                    call void* Testing.Class1::GetAddr(object&)
                    stloc.1
                    ldc.i4.5
                    stloc.2
                    ldloca 2 //note ldloca for value types
                    call void* Testing.Class1::GetAddr(object&)
                    stloc.1
                    ret
                    }

                    .method private hidebysig static void\* 
                            GetAddr(object& o) cil managed
                    {
                      .maxstack  1
                      ldarg.0
                      conv.u
                      ret
                    }
                    
                    .method public hidebysig specialname rtspecialname 
                            instance void  .ctor() cil managed
                    {
                      .maxstack  1
                      ldarg.0
                      call       instance void \[mscorlib\]System.Object::.ctor()
                      ret
                    }
                    

                    }
                    }

                    I dont know how one would call a ref object without adding the ref keyword in C#??? leppie::AllocCPArticle(Generic DFA State Machine for .NET);

                    G 1 Reply Last reply
                    0
                    • L leppie

                      Here is a full sample, see the comments:

                      .assembly extern mscorlib
                      {
                      .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )
                      .ver 1:0:3300:0
                      }
                      .assembly Testing
                      {
                      .hash algorithm 0x00008004
                      .ver 1:0:1272:38245
                      }
                      .module Testing.exe
                      .custom instance void [mscorlib]System.Security.UnverifiableCodeAttribute::.ctor() = ( 01 00 00 00 )
                      .imagebase 0x00400000
                      .subsystem 0x00000003
                      .file alignment 512
                      .corflags 0x00000001

                      .namespace Testing
                      {
                      .class private auto ansi beforefieldinit Class1
                      extends [mscorlib]System.Object
                      {
                      .method private hidebysig static void
                      Main(string[] args) cil managed
                      {
                      .entrypoint
                      .custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01 00 00 00 )
                      .maxstack 1
                      .locals ([0] object c1,
                      [1] void* h,
                      [2] int32 i)
                      newobj instance void Testing.Class1::.ctor()
                      stloc.0
                      ldloc.s c1 //ldloca will box ref type
                      call void* Testing.Class1::GetAddr(object&)
                      stloc.1
                      ldc.i4.5
                      stloc.2
                      ldloca 2 //note ldloca for value types
                      call void* Testing.Class1::GetAddr(object&)
                      stloc.1
                      ret
                      }

                      .method private hidebysig static void\* 
                              GetAddr(object& o) cil managed
                      {
                        .maxstack  1
                        ldarg.0
                        conv.u
                        ret
                      }
                      
                      .method public hidebysig specialname rtspecialname 
                              instance void  .ctor() cil managed
                      {
                        .maxstack  1
                        ldarg.0
                        call       instance void \[mscorlib\]System.Object::.ctor()
                        ret
                      }
                      

                      }
                      }

                      I dont know how one would call a ref object without adding the ref keyword in C#??? leppie::AllocCPArticle(Generic DFA State Machine for .NET);

                      G Offline
                      G Offline
                      GriffonRL
                      wrote on last edited by
                      #10

                      Hi Leppie, I'm amazed :omg: ! Thank you very much for this. Coding at the IL level looks very hardcore for me ;). Good for you. .NET has no secret for you :-D. Cheers, R. LOPES Just programmer.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups