I only allocate 16 ints, but can set the value of the 10000 int. Why does this program runs fine without memory error ?
#nowarn "9"
open System
open System.Runtime.InteropServices
open Microsoft.FSharp.NativeInterop
let si:int=sizeof<int>
let nulpointer:nativeptr<int> = NativePtr.ofNativeInt<int> 0
type MyClass() =
do printfn "Create"
let mutable ptypedback:nativeptr<int>=nulpointer
member this.puntyped: nativeint = Marshal.AllocHGlobal (si*16)
member this.ptyped
with get (): nativeptr<int> = ptypedback
and set (value: nativeptr<int>) = ptypedback <- value
member this.setptr = this.ptyped
<- NativePtr.ofNativeInt<int> this.puntyped
interface IDisposable with
member this.Dispose() =
Marshal.FreeHGlobal this.puntyped
printfn "Destroy"
let myfun =
use c = new MyClass()
c.setptr
let set10000 = NativePtr.set c.ptyped 10000
set10000 123
let get10000 =NativePtr.get c.ptyped 10000
printfn "Value at index 10000 : %A " get10000
myfun