3 ms·
> I'm not sure about C# (the only other language I saw with interior pointers). I think C# semi-recently switched over to a moving collector. In which case, I'm
by Rohansi 8mo ago
> I'm not sure about C# (the only other language I saw with interior pointers). I think C# semi-recently switched over to a moving collector. In which case, I'm curious to know how they solved the interior pointer problem.
Objects references are just pointers in .NET. See the JIT disassembly below. It's been using a moving GC for a long time, too.
https://sharplab.io/#v2:C4LghgzgtgNAJiA1AHwAICYCMBYAUKgZgAIMiBhIgbzyNpOIFcA7CMAMwFMSAWIgWQAUASio064gG5gATkThhgYIgF4iTDgHciAIwCewDgG1MABhMBdANxjxteYsMB2cyqIBOa7lt0psgB6uqJgAdABKzMAAllAcwWQA9lAADpEANhzSAMoZEpEAxhwQwQCqLOyxAIIQAArxkUwG0gLSHGxyCmCGAKzmQp7iAL54A0A= https://sharplab.io/#v2:C4LghgzgtgNAJiA1AHwAICYCMBYAUKgZgAIM...
- cogman10 8mo agoInteresting. I wonder how C# handles the moving. I'm guessing it has to go in and fix up the pointers in the stack after a gc run? Or is this some OS level virtual pointer weirdness going on? How does C# guard against someone doing something silly like turning a pointer into a long and then back into a pointer again later?
- Rohansi 8mo agoThe runtime knows exactly where GC pointers are so I would assume that is what it does. It even knows precisely when locals are no longer needed so it can stop treating objects they refer to as reachable. It's instruction level, not based on scopes, so an object can be freed while it is still in scope if the code doesn't access it! > How does C# guard against someone doing something silly like turning a pointer into a long and then back into a pointer again later? I don't think it does. You can't do most of these things without using unsafe code, which needs a compiler flag enabled and code regions marked as `unsafe`.