Skip to content

Instantly share code, notes, and snippets.

@Const-me
Created September 29, 2020 08:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Const-me/9ae865dd94df0a7423aa054cf76bf101 to your computer and use it in GitHub Desktop.
Save Const-me/9ae865dd94df0a7423aa054cf76bf101 to your computer and use it in GitHub Desktop.
using System;
using System.Diagnostics;
using System.IO.MemoryMappedFiles;
using System.Runtime.InteropServices;
namespace MemoryMapBug
{
[StructLayout( LayoutKind.Sequential, Pack = 1 )]
struct sTest
{
public int f1;
public byte f2;
}
static class Program
{
const int length = 1024 * 64;
static void test()
{
int cb = Marshal.SizeOf( typeof( sTest ) );
Debug.Assert( cb == 5 );
MemoryMappedFile mmf = MemoryMappedFile.CreateNew( null, cb * length );
var acc = mmf.CreateViewAccessor();
// Console.WriteLine( "Capacity: {0}", acc.Capacity );
sTest[] arr = new sTest[ length ];
for( int i = 0; i < length; i++ )
arr[ i ] = new sTest() { f1 = i, f2 = (byte)( i & 0xFF ) };
acc.WriteArray( 0, arr, 0, length );
}
static void Main( string[] args )
{
try
{
test();
Console.WriteLine( "Completed OK" );
}
catch( Exception ex )
{
Console.WriteLine( "Crashed: {0}", ex.Message );
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment