On Monday, 18 August 2025 at 15:26:21 UTC, monkyyy wrote:
On Monday, 18 August 2025 at 07:32:13 UTC, Steven Schveighoffer wrote:
2. You can store data in the lower bits of an aligned pointer (after all, this is just an interior pointer).

Will the gc count this as a reference while scaning in any case?

As long as it's stored as a pointer, it is treated as a pointer.

I'll try my hand at ascii art.

Let's say you have an integer 0x4321 allocated in memory at address 0x100:

```
         -------------------------
Address: | 100 | 101 | 102 | 103 |
         -------------------------
   Data: |  1  |  2  |  3  |  4  |
         -------------------------
```

A pointer to the integer points at address 0x100

```
         -------------------------
Address: | 100 | 101 | 102 | 103 |
         -------------------------
   Data: |  1  |  2  |  3  |  4  |
         -------------------------
            ^ ptr
```

Now, you add a 1 to the pointer value to have it pointing at 0x101, it looks like this:

```
         -------------------------
Address: | 100 | 101 | 102 | 103 |
         -------------------------
   Data: |  1  |  2  |  3  |  4  |
         -------------------------
                  ^ ptr
```

This is still pointing at the integer. So the GC believes the integer memory block is still valid because there's a pointer interior to the block

Add 2, and you get a pointer to the `3` byte, add 3 and you get a pointer to the `4` byte. Add 4, and now it's pointing at the next integer, and so now it's no longer pointing at the integer, and this becomes invalid.

So you have 2 bits of space you can use to store a value from 0 to 3, and still have it be a valid pointer.

-Steve
  • Re: Pointers - I... H. S. Teoh via Digitalmars-d-learn
  • Re: Pointers - I... Brother Bill via Digitalmars-d-learn
    • Re: Pointer... H. S. Teoh via Digitalmars-d-learn
    • Re: Pointer... Brother Bill via Digitalmars-d-learn
      • Re: Poi... Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn
        • Re:... Brother Bill via Digitalmars-d-learn
          • ... Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn
          • ... monkyyy via Digitalmars-d-learn
          • ... Steven Schveighoffer via Digitalmars-d-learn
          • ... monkyyy via Digitalmars-d-learn
          • ... Steven Schveighoffer via Digitalmars-d-learn
          • ... monkyyy via Digitalmars-d-learn
        • Re:... 0xEAB via Digitalmars-d-learn
          • ... 0xEAB via Digitalmars-d-learn
          • ... Brother Bill via Digitalmars-d-learn
          • ... Monkyyy via Digitalmars-d-learn
          • ... Brother Bill via Digitalmars-d-learn
          • ... Monkyyy via Digitalmars-d-learn
      • Re: Poi... Monkyyy via Digitalmars-d-learn
      • Re: Poi... Ali Çehreli via Digitalmars-d-learn
        • Re:... Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn

Reply via email to