Red Gate forums :: View topic - Pointers
Return to www.red-gate.com RSS Feed Available

Search  | Usergroups |  Profile |  Messages |  Log in  Register 
Go to product documentation
.NET Reflector 7 EAP
.NET Reflector 7 EAP forum

Pointers

Search in .NET Reflector 7 EAP forum
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.
Jump to:  
Author Message
z0rak



Joined: 15 Feb 2011
Posts: 1

PostPosted: Tue Feb 15, 2011 7:54 pm    Post subject: Pointers Reply with quote

Compiled this source:
Code:

        unsafe static void _method1(int val)
        {
            int* x = &val;
            for (int i = 0; i < 10; i++)
            {
                x = x + 1; // increment x (add 4 to the pointer)
            }
        }
        unsafe static void _method2(int val)
        {
            int* x = &val;
            for (int i = 0; i < 10; i++)
            {
                x = (int*)(((byte*)x) + 1); // cast to a byte* and increment x (add 1 to the pointer)
            }
        }


Reflector decompiles it to this (both methods are exactly the same):
Code:

    private static unsafe void _method1(int val)
    {
        int* x = &val;
        for (int i = 0; i < 10; i++)
        {
            x++;
        }
    }

    private static unsafe void _method2(int val)
    {
        int* x = &val;
        for (int i = 0; i < 10; i++)
        {
            x++;
        }
    }



Here's the disassembly as IL
Code:

    .method private hidebysig static void _method1(int32 val) cil managed
    {
        .maxstack 2
        .locals init (
            [0] int32* x,
            [1] int32 i,
            [2] bool CS$4$0000)
        L_0000: nop
        L_0001: ldarga.s val
        L_0003: conv.u
        L_0004: stloc.0
        L_0005: ldc.i4.0
        L_0006: stloc.1
        L_0007: br.s L_0014
        L_0009: nop
        L_000a: ldloc.0
>>>
        L_000b: ldc.i4.4
>>>
        L_000c: conv.i
        L_000d: add
        L_000e: stloc.0
        L_000f: nop
        L_0010: ldloc.1
        L_0011: ldc.i4.1
        L_0012: add
        L_0013: stloc.1
        L_0014: ldloc.1
        L_0015: ldc.i4.s 10
        L_0017: clt
        L_0019: stloc.2
        L_001a: ldloc.2
        L_001b: brtrue.s L_0009
        L_001d: ret
    }

    .method private hidebysig static void _method2(int32 val) cil managed
    {
        .maxstack 2
        .locals init (
            [0] int32* x,
            [1] int32 i,
            [2] bool CS$4$0000)
        L_0000: nop
        L_0001: ldarga.s val
        L_0003: conv.u
        L_0004: stloc.0
        L_0005: ldc.i4.0
        L_0006: stloc.1
        L_0007: br.s L_0014
        L_0009: nop
        L_000a: ldloc.0
>>>
        L_000b: ldc.i4.1
>>>
        L_000c: conv.i
        L_000d: add
        L_000e: stloc.0
        L_000f: nop
        L_0010: ldloc.1
        L_0011: ldc.i4.1
        L_0012: add
        L_0013: stloc.1
        L_0014: ldloc.1
        L_0015: ldc.i4.s 10
        L_0017: clt
        L_0019: stloc.2
        L_001a: ldloc.2
        L_001b: brtrue.s L_0009
        L_001d: ret
    }


The subtle difference is the casting the pointer from (int*) to (byte*) before incrementing in _method2
Back to top
View user's profile Send private message
Clive Tong



Joined: 04 Dec 2008
Posts: 283

PostPosted: Thu Feb 17, 2011 9:49 am    Post subject: Reply with quote

I've logged that in our bug tracking system as RP-1075.
Back to top
View user's profile Send private message
Display posts from previous:   
This topic is locked: you cannot edit posts or make replies. All times are GMT + 1 Hour
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group