# What pointer arithmetic looks like decompiled


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](https://delta1labs.com/products/glass) 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:

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

The body compiles to:

```cil
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:

```csharp
static unsafe int Get(int* p, int i) => p[i];
```

```cil
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:

```csharp
static unsafe void Set(int* p, int i, int v) => p[i] = v;
```

```cil
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:

```csharp
static unsafe T Get<T>(T* p, int i) where T : unmanaged => p[i];
```

```cil
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.

<figure class="my-8"><svg viewBox="0 0 760 300" aria-label="The C# expression p[i] lowering to a sizeof-scaled add plus ldind, and Glass recovering the indexer" role="img"><rect x="0" y="0" width="760" height="300" fill="none"></rect><defs><marker id="pa" markerWidth="9" markerHeight="9" refX="7" refY="3" orient="auto"><path d="M0,0 L7,3 L0,6 Z" fill="#64748b"></path></marker></defs><text x="20" y="26" fill="#64748b" font-family="sans-serif" font-size="13" font-weight="bold">source</text><rect x="20" y="40" width="190" height="44" rx="6" fill="none" stroke="#6366f1" stroke-width="1.5"></rect><text x="36" y="67" fill="#6366f1" font-family="monospace" font-size="15">int v = p[i];</text><text x="20" y="120" fill="#64748b" font-family="sans-serif" font-size="13" font-weight="bold">lowered to IL</text><rect x="20" y="134" width="300" height="140" rx="6" fill="none" stroke="#0891b2" stroke-width="1.5"></rect><text x="36" y="158" fill="#0891b2" font-family="monospace" font-size="12">ldarg.0        // p</text><text x="36" y="178" fill="#0891b2" font-family="monospace" font-size="12">ldarg.1        // i</text><text x="36" y="198" fill="#0891b2" font-family="monospace" font-size="12">conv.i         // i -> native int</text><text x="36" y="218" fill="#0891b2" font-family="monospace" font-size="12">ldc.i4.4       // sizeof(int)</text><text x="36" y="238" fill="#0891b2" font-family="monospace" font-size="12">mul   add      // p + i*4</text><text x="36" y="258" fill="#0891b2" font-family="monospace" font-size="12">ldind.i4       // *(p + i*4)</text><path d="M 332 200 L 396 200" stroke="#64748b" stroke-width="1.5" fill="none" marker-end="url(#pa)"></path><rect x="402" y="176" width="150" height="48" rx="6" fill="#6366f1" stroke="none"></rect><text x="477" y="198" fill="#ffffff" font-family="sans-serif" font-size="12" text-anchor="middle" font-weight="bold">Glass</text><text x="477" y="214" fill="#ffffff" font-family="sans-serif" font-size="11" text-anchor="middle">pattern match</text><path d="M 556 200 L 596 200" stroke="#64748b" stroke-width="1.5" fill="none" marker-end="url(#pa)"></path><rect x="602" y="150" width="150" height="100" rx="6" fill="none" stroke="#10b981" stroke-width="1.5"></rect><text x="618" y="186" fill="#10b981" font-family="monospace" font-size="14">p[i]</text><text x="618" y="212" fill="#64748b" font-family="sans-serif" font-size="11">element = int,</text><text x="618" y="228" fill="#64748b" font-family="sans-serif" font-size="11">from .i4 width</text></svg><figcaption class="text-sm text-muted mt-2">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.</figcaption></figure>

## 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:

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

static unsafe int ReadValue(Node* n) => n->Value;
```

```cil
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:

```csharp
static unsafe int* AddressOfField(Node* n) => &n->Value;
```

```cil
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:

```csharp
static unsafe long Distance(int* a, int* b) => b - a;
```

```cil
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 `int`s; `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.

