IntPtr.Subtract(IntPtr, Int32) 메서드

정의

부속 정수에서 오프셋을 뺍니다.

public:
 static IntPtr Subtract(IntPtr pointer, int offset);
public static IntPtr Subtract (IntPtr pointer, int offset);
static member Subtract : nativeint * int -> nativeint
Public Shared Function Subtract (pointer As IntPtr, offset As Integer) As IntPtr

매개 변수

pointer
IntPtr

nativeint

오프셋을 뺍니다.

offset
Int32

뺄 오프셋입니다.

반환

IntPtr

nativeint

에서pointer의 빼기를 offset 반영하는 새 부속 정수입니다.

예제

다음 예제에서는 10개 요소 배열의 끝을 가리키는 개체를 인스턴스화 IntPtr 한 다음 메서드를 호출 Subtract 하여 배열의 요소를 역순으로 반복합니다.

using System;
using System.Runtime.InteropServices;

public class Example
{
   public static void Main()
   {
      int[] arr = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
      // Get the size of a single array element.
      int size = sizeof(int);
      unsafe {
         fixed(int* pend = &arr[arr.GetUpperBound(0)]) {
            IntPtr ptr = new IntPtr(pend);
            for (int ctr = 0; ctr < arr.Length; ctr++)
            {
               IntPtr newPtr = IntPtr.Subtract(ptr, ctr * size);
               Console.Write("{0}   ", Marshal.ReadInt32(newPtr));
            }
         }
      }
   }
}
// The example displays the following output:
//       20   18   16   14   12   10   8   6   4   2
#nowarn "9"
open System
open System.Runtime.InteropServices
open FSharp.NativeInterop

[<EntryPoint>]
let main _ =
    let arr =
        [| 2; 4; 6; 8; 10; 12; 14; 16; 18; 20 |]

    // Get the size of a single array element.
    let size = sizeof<int>

    use pend = fixed &arr[arr.GetUpperBound 0]
    let ptr = NativePtr.toNativeInt pend 
    for i = 0 to arr.Length - 1 do
        let newPtr = IntPtr.Subtract(ptr, i * size)
        printf $"{Marshal.ReadInt32 newPtr}   "
    0

// The example displays the following output:
//       20   18   16   14   12   10   8   6   4   2
Imports System.Runtime.InteropServices

Module Example
   Public Sub Main()
      Dim arr() As Integer = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20}
      Dim ptr As IntPtr = Marshal.UnsafeAddrOfPinnedArrayElement(arr, arr.Length - 1)
      Dim size As Integer = Len(arr(0))

      For ctr As Integer = 0 To arr.Length - 1
         Dim newPtr As IntPtr = IntPtr.Subtract(ptr, ctr * size)
         Console.Write("{0}   ", Marshal.ReadInt32(newPtr))
      Next
   End Sub
End Module
' The example displays the following output:
'       20   18   16   14   12   10   8   6   4   2

설명

Subtract 결과가 너무 작아서 실행 프로세스에서 부 서명된 정수로 나타낼 수 없는 경우 메서드는 예외를 throw하지 않습니다. 대신 빼기 작업은 선택되지 않은 컨텍스트에서 수행됩니다.

연산자 오버로드 또는 사용자 지정 연산자를 지원하지 않는 언어는 이 메서드를 사용하여 포인터 값에서 오프셋을 뺄 수 있습니다.

적용 대상

추가 정보