Re:Baby's First Garbage Collector
by kevingadd on Sunday December 08, 2013 @10:22PM
(#6872696)
You can't store a raw pointer, but you can store the 'safe' forms of pointers (IntPtr, etc) which allows you to do some of the stuff you'd want to do - allocate a buffer of given size in unmanaged heap, then manipulate it entirely using struct pointers and pointer arithmetic. The key/value store I wrote in C# does this - no data lives in the managed heap, it's all sitting in unmanaged memory (mapped files, as it happens).
[ Parent
| on HN ]
Re:Baby's First Garbage Collector
by MaulingMonkey on Sunday December 08, 2013 @10:35PM
(#6872718)
> You can't store a raw pointer
You can if you mark the containing class/struct "unsafe", which allows both type safety and increased ease of use.
Hmmm, I've never seen this done. Thanks for the heads-up, I'll have to try it out. Are you sure you can do it with classes? I would not be shocked by being able to do it with structs.
Re:Baby's First Garbage Collector
by MaulingMonkey on Sunday December 08, 2013 @11:36PM
(#6872832)
Sure am.
using System;
namespace Test2012
{
unsafe class C { public byte* P; public override string ToString( ) { return new IntPtr(P).ToString( ); } }
unsafe struct S { public byte* P; public override string ToString( ) { return new IntPtr(P).ToString( ); } }
static class Program
{
static unsafe void Main( )
{
fixed( byte* test = new byte[] { 1, 2, 3 } )
{
var c = new C( ) { P = test };
var s = new S( ) { P = test };
Console.WriteLine( "Test: {0}", c );
Console.WriteLine( "Test: {0}", s );
}
}
}
}
Result: Test: 40510364
Test: 40510364
Press any key to continue . . .