nodakai
nodakai

Reputation: 8033

.Net System.Span<T>: does it pin the underlying object and protect it from compaction?

Perhaps the answer is "depends on which ctor was used"?

Span<T>(Void*, Int32): I'm sure it doesn't (can't) pin... How about Span<T>(T[]) and its cousin Span<T>(T[], Int32, Int32)?

By "pinning" I mean How can I pin an array of byte?

Upvotes: 1

Views: 373

Answers (1)

Ben Voigt
Ben Voigt

Reputation: 283793

No, Span<T> doesn't pin anything. If it did, what would be the point of Span<T>.GetPinnableReference() ?

Note that the documentation for GetPinnableReference() says

Applications should not directly call GetPinnableReference. Instead, callers should use their language's normal pinning syntax, such as C#'s fixed statement.

From that we can again infer than an object is not pinned by Span<T> alone, but is pinned if fixed is used.

Upvotes: 2

Related Questions