Skip to content
← All posts
· Delta1 Labs Glass.NETDecompilationC#IL

What pointer arithmetic looks like decompiled

Raw pointer code carries no array or offset syntax in IL — just ldind.i4/stind.i4 loads and stores, a sizeof-scaled add behind every p[i] = *(p + i), ldflda for p->Field, and conv.u where an address becomes a pointer. Here is the IL for a tiny unsafe method and how Glass.NET infers the element type from the opcode width.

Pointer code in C# is dense with syntax: the * dereference, the & address-of, the [i] indexer, the -> arrow. None of it survives to IL. A method full of unsafe pointer work compiles to a flat stream of integer arithmetic and ldind/stind opcodes, with the pointer-ness living only in the signature types and the width of each load. Point a decompiler at it and the interesting question is how much of that syntax comes back — and how much it has to infer, because the IL does not record it. This post walks the IL for a few tiny unsafe methods and shows what Glass.NET reconstructs, and where the reconstruction has to guess.

The focus here is raw pointer arithmetic in code that is already unsafe and, where needed, already pinned. Turning a managed reference into a pinned pointer with the fixed statement, and the interop marshalling around it, are their own mechanisms covered elsewhere — this is about what happens to p, p[i], p->Field and p2 - p1 once you already hold a pointer.

Taking an address: ldloca and conv.u

Start with the smallest thing that makes a pointer — the address of a local:

static unsafe int BumpLocal()
{
    int x = 10;
    int* p = &x;
    *p = *p + 5;
    return x;
}

The body compiles to:

ldc.i4.s   10
stloc.0          // x = 10
ldloca.s   0     // &x  -> int& (managed pointer)
conv.u           // int& -> int* (unmanaged pointer)
stloc.1          // p
ldloc.1          // p
ldloc.1          // p
ldind.i4         // *p   -> load four signed bytes
ldc.i4.5
add
stind.i4         // *p = ...  -> store four bytes
ldloc.0
ret

Two opcodes carry the pointer semantics. ldloca.s 0 pushes the address of local 0 — that is a managed pointer (int&), the verifiable, GC-tracked kind. conv.u converts it to an unmanaged native unsigned int — the raw int*. That conv.u is the moment a tracked reference becomes an opaque pointer; it is the fingerprint of &localOrField feeding a pointer variable. Then ldind.i4 reads four bytes through the pointer and stind.i4 writes four back. The .i4 suffix is the whole story about the element type: four signed bytes, i.e. int.

Indexing: p[i] is *(p + i)

There is no pointer-indexer opcode. p[i] is defined as *(p + i), and that addition is ordinary integer arithmetic on the address:

static unsafe int Get(int* p, int i) => p[i];
ldarg.0          // p
ldarg.1          // i
conv.i           // widen index to native int
ldc.i4.4         // sizeof(int) folded to a constant
mul              // i * 4
add              // p + i*4
ldind.i4         // *(p + i*4)
ret

Read it in order: push the base pointer, push the index, widen the index to native int with conv.i (so the arithmetic is pointer-width on 64-bit), scale it by the element size, add it to the base, and ldind.i4 the result. The [i] and the + have both dissolved into conv.i; ldc.i4.4; mul; add. Storing is the mirror image:

static unsafe void Set(int* p, int i, int v) => p[i] = v;
ldarg.0
ldarg.1
conv.i
ldc.i4.4
mul
add
ldarg.2          // v
stind.i4         // *(p + i*4) = v
ret

Same address computation, then stind.i4 instead of ldind.i4. The scale ldc.i4.4 is a folded constant: the compiler knew sizeof(int) at compile time and baked 4 in. For a pointer to a generic unmanaged type, where the size is not known until the type argument arrives, the sizeof opcode survives instead:

static unsafe T Get<T>(T* p, int i) where T : unmanaged => p[i];
ldarg.0
ldarg.1
conv.i
sizeof     !!T   // element size, computed at runtime
mul
add
ldobj      !!T   // generic load (ldind has no generic form)
ret

Here the scale is a real sizeof opcode over the type parameter, and the load is ldobj !!T because ldind has no generic variant. Both forms mean the same thing — p[i] — but the IL view looks different.

sourceint v = p[i];lowered to ILldarg.0 // pldarg.1 // iconv.i // i -> native intldc.i4.4 // sizeof(int)mul add // p + i*4ldind.i4 // *(p + i*4)Glasspattern matchp[i]element = int,from .i4 width
p[i] has no indexer opcode: it is a sizeof-scaled add feeding ldind.i4. Glass reads the .i4 width back to an int element and reprints the indexer.

The ldind/stind family is the type channel

Inside a method body the element type is carried almost entirely by the width of the indirection opcode. The family:

  • ldind.i1 / stind.i1 — one byte (sbyte; bool/byte use ldind.u1)
  • ldind.i2 / stind.i2 — two bytes (short; ushort/char via ldind.u2)
  • ldind.i4 / stind.i4 — four bytes (int)
  • ldind.i8 / stind.i8 — eight bytes (long)
  • ldind.r4 / ldind.r8 — float / double
  • ldind.ref / stind.ref — a managed object reference

A decompiler reads the width and prints the matching element type. That mapping is what lets Glass turn add; ldind.i4 back into p[i] on an int* rather than leaving raw indirection.

Member access through a pointer

p->Field is defined as (*p).Field. A read uses ldfld applied directly to the pointer — ldfld accepts an address on the stack, so the struct never has to be copied:

struct Node { public int Value; public int Next; }

static unsafe int ReadValue(Node* n) => n->Value;
ldarg.0
ldfld      int32 Node::Value
ret

When you need the address of a field reached through a pointer — &n->Value, or a field used as a further pointer base — you get ldflda (load field address) on the pointer instead:

static unsafe int* AddressOfField(Node* n) => &n->Value;
ldarg.0
ldflda     int32 Node::Value
conv.u
ret

ldflda yields the managed pointer to the field; conv.u turns it into the int* the signature promises — the same reference-to-pointer step as the earlier &x. Glass reads ldflda on a pointer-typed receiver back into &n->Field, and plain ldfld/stfld on a pointer back into n->Field.

Pointer difference

Subtracting two pointers yields an element count, not a byte count, so the compiler divides the byte difference by the element size:

static unsafe long Distance(int* a, int* b) => b - a;
ldarg.1          // b
ldarg.0          // a
sub              // byte difference (native int)
ldc.i4.4         // sizeof(int)
div              // / element size  -> element count
conv.i8          // result is long
ret

sub gives the raw byte distance; the div by 4 converts it to a count of ints; conv.i8 widens to the long that p2 - p1 is typed as. Glass recognises subtract-two-pointers-then-divide-by-element-size and reprints b - a rather than the byte arithmetic. The divisor is the same scale factor as in indexing — a folded constant for a known type, a sizeof opcode for a generic one.

When the recovery is a guess

The honest caveats all come from the IL recording less than the source said:

  • Signed vs unsigned is ambiguous. ldind.i4 is emitted both for an int* load and for reading an int-typed value through arithmetic over a uint*; the opcode width is four bytes either way. When a parameter signature pins the type, Glass uses it; when the only evidence is the opcode, it favours the signed spelling (int), which may not be what the author wrote. The same four-byte width covers an int* and a uint* whenever no signature disambiguates.
  • void* and reinterpret casts lose type information. A (int*) cast over a void* that does not change the access width leaves no opcode behind; after the cast the only type signal is the eventual ldind/stind. Glass reports the width it finds and cannot always recover which cast the author wrote to get there.
  • Scaling can be invisible. For a known element type the scale is a folded ldc.i4.4, not a sizeof opcode, so p[i] and the hand-written *(p + i * 4) compile to the same IL. Glass prefers the indexer form, but the two sources are indistinguishable at the IL level — the folded constant is all that remains of sizeof(int).
  • No checked/unchecked to read. Pointer arithmetic is never overflow-checked, so the scaling add, mul and sub are always the plain opcodes. There is no add.ovf on pointer code for a decompiler to reflect, and Glass never wraps recovered pointer expressions in checked.

Reading it yourself

Open the assembly in Glass, select an unsafe method, and switch between the C# and IL views. In IL you will see the arithmetic skeleton — conv.u where an address became a pointer, conv.i; mul; add behind every index, ldind/stind with the width that names the element type, ldflda behind &p->Field, and the sub; div of a pointer difference. In C# you will see those collapsed back into *p, p[i], p->Field and b - a. Flip between them on one method and the lowering stops being abstract: each piece of syntax maps to a specific run of opcodes, and the width suffix is the single most important clue the decompiler has.

Glass.NET is a free .NET decompiler for code you are entitled to inspect — your own assemblies, your dependencies, and third-party binaries you are licensed to analyse. Raw pointer code is where a decompiler earns its keep, because the source syntax is gone and only the arithmetic and the opcode widths remain; reading those back into p[i] and p->Field is exactly the work Glass does so the unsafe method reads like the code the author wrote.

Try Nebula.NET

Harden your .NET code in minutes — start with the free edition.