고정 키워드(keyword)
fixed
키워드(keyword) 통해 로컬을 스택에 "고정"하여 가비지 수집 중에 수집되거나 이동되지 않도록 할 수 있습니다. 하위 수준 프로그래밍 시나리오에 사용됩니다.
구문
use ptr = fixed expression
설명
이렇게 하면 포인터를 추출하고 가비지 수집 중에 수집되거나 이동되지 않는 이름에 바인딩할 수 있도록 식 구문을 확장합니다.
식의 포인터는 키워드(keyword) 통해 fixed
고정되며 키워드(keyword) 통해 use
식별자에 바인딩됩니다. 이 의미 체계는 키워드(keyword) 통해 리소스 관리와 use
유사합니다. 포인터는 범위에 있는 동안 고정되고 범위를 벗어나면 더 이상 고정되지 않습니다. fixed
바인딩의 use
컨텍스트 외부에서 사용할 수 없습니다. 을 사용하여 이름 use
에 포인터를 바인딩해야 합니다.
함수 또는 메서드의 fixed
식 내에서 사용해야 합니다. 스크립트 수준 또는 모듈 수준 범위에서는 사용할 수 없습니다.
모든 포인터 코드와 마찬가지로 이는 안전하지 않은 기능이며 사용될 때 경고를 내보낸다.
예시
open Microsoft.FSharp.NativeInterop
type Point = { mutable X: int; mutable Y: int}
let squareWithPointer (p: nativeptr<int>) =
// Dereference the pointer at the 0th address.
let mutable value = NativePtr.get p 0
// Perform some work
value <- value * value
// Set the value in the pointer at the 0th address.
NativePtr.set p 0 value
let pnt = { X = 1; Y = 2 }
printfn $"pnt before - X: %d{pnt.X} Y: %d{pnt.Y}" // prints 1 and 2
// Note that the use of 'fixed' is inside a function.
// You cannot fix a pointer at a script-level or module-level scope.
let doPointerWork() =
use ptr = fixed &pnt.Y
// Square the Y value
squareWithPointer ptr
printfn $"pnt after - X: %d{pnt.X} Y: %d{pnt.Y}" // prints 1 and 4
doPointerWork()
참고 항목
GitHub에서 Microsoft와 공동 작업
이 콘텐츠의 원본은 GitHub에서 찾을 수 있으며, 여기서 문제와 끌어오기 요청을 만들고 검토할 수도 있습니다. 자세한 내용은 참여자 가이드를 참조하세요.
.NET