Questions tagged [unsafe-pointers]

Some "safe" languages like Swift and Go offer "unsafe" pointers for use with APIs written in languages like C and Objective-C. Questions using this tag should also have the relevant language tag, e.g. [swift] or [go].

250 questions
0
votes
1 answer

You can only take address of an unfixed expression inside of a fixed statement initializer error in c#

I did reverse engineering of a project using .NET Reflector everything is fine Instead of this error I don't know how I can solve it please guide me Thanks in Advance. Here is the code. CS0212 You can only take the address of an unfixed…
buddy
  • 418
  • 4
  • 10
  • 29
0
votes
0 answers

Should a reference obtained via MemoryMarshal.GetReference() be pinned?

I am looking at the use of System.Runtime.InteropServices.MemoryMarshal.GetReference() in the dotnet runtime repository, and I don't see any explicit pinning of the obtained references being done. Which suggests that maybe the object we have…
redcalx
  • 8,177
  • 4
  • 56
  • 105
0
votes
1 answer

How to create instance of Swift type UnsafePointer?>! to pass to C function

I am trying to call a C function from Swift One of the arguments to the C function is defined as the type: const char * const * i_argv In Xcode auto completion maps this to Swift type: _ i_argv: UnsafePointer?>! All my attempts…
novocodev
  • 3
  • 3
0
votes
1 answer

How to assign a 2D array into a pointer in a fixed statement

I have a question about a code that I use. If we look at the line: fixed (byte* fixedInput = &array2D[5, 0]) Here I assign the 5th index in the array2D to the pointer fixedInput. Complete code: public unsafe static void testFunction() { …
Andreas
  • 1,121
  • 4
  • 17
  • 34
0
votes
1 answer

Calling C function with array pointer and int pointer from Swift

I am creating a C lib plus a wrapper for easy use in Swift. The C function takes two parameters, an array pointer and an int pointer: int crgetproclist(struct kinfo_proc *proc_list, size_t *count) { int err = 0; size_t length = 0; …
Cravid
  • 653
  • 2
  • 7
  • 22
0
votes
1 answer

Issue updating code to Swift 5.1 'withUnsafeBytes is deprecated: use withUnsafeBytes(_: (UnsafeRawBufferPointer) throws -> R) rethrows -> R instead'

I'm using a simple extension to pack and unpack data, the code below currently still works but it's throwing me a warning and I'd like to update it to prevent issues down the line. There are two similar questions asked about this, but I haven't been…
Prolix
  • 169
  • 7
0
votes
0 answers

Understanding the performance of data copying in unsafe swift

I'm finally familiar enough with Unsafe…Pointer types in Swift to try to address how the following code was marked as unsafe by XCode. let pixelBuffer = UnsafeMutablePointer(&pixels) let bytes =…
0
votes
0 answers

SwiftUI doesn't refresh with an array initialized by a pointer or a NSMutableArray

I know, this is a very unusual thing to do, but I noticed that SwiftUI view won't update when a @State property is assigned from a pointer: struct ContentView: View { @State private var numbers: [Int32] = [] var body: some View { …
Jay Lee
  • 1,684
  • 1
  • 15
  • 27
0
votes
1 answer

How to assign 1st dimension of a 2D array to a pointer

I have a 2D array and I am trying to assign the 1st dimension of that 2D array to a pointer like below but that doesn't work. fixed (byte* fixedInput = array2D[0]) How can I assign only the first dimension like I am trying to do to…
Andreas
  • 1,121
  • 4
  • 17
  • 34
0
votes
0 answers

How do unsafe code work exactly. Is it okay to mix managed resources in a fixed code

I have a question about unsafe code functions. As I have understand I need to use it because I use pointers. I am not completely sure how it works. So I will try with some question marks to see if I can sort it out better. Is it true that unsafe…
Andreas
  • 1,121
  • 4
  • 17
  • 34
0
votes
1 answer

How to extract UnsafePointer from UnsafePointer - Swift

I’m playing around with learning about pointers in Swift. For instance, this code starts with a CGPoint array, creates an UnsafePointer and then extracts all the x values into a CGFloat array: import Foundation let points = [CGPoint(x:1.2, y:3.33),…
koen
  • 5,383
  • 7
  • 50
  • 89
0
votes
1 answer

Is there a way to utilize Swift commands within a low-level C method call?

I am maintaining a set of data obtained from a AVCaptureSynchronizedData. One of the methods I use modifies the CVPixelBuffers obtained from the AVCaptureSynchronizedData. While modifying. the CVPixelBuffer, I create a copy of the CVPixelBuffer via…
impression7vx
  • 1,728
  • 1
  • 20
  • 50
0
votes
1 answer

When is a fixed size array allocated on the stack?

I have the following method to copy bytes from a socket stream to disk: public static void CopyStream(Stream input, Stream output) { // Insert null checking here for production byte[] buffer = new byte[8192]; int bytesRead; while…
Jacko
  • 12,665
  • 18
  • 75
  • 126
0
votes
2 answers

How to pass argument in Swift to C function that takes an UnsafePointer?

I've imported a C function called geoToH3 that returns an H3Index (which is just a UInt64). let h3Index = geoToH3(g: UnsafePointer!, r: Int32) The function takes an Int32 and a GeoCoord object, which is just an object with a pair of…
lurning too koad
  • 2,698
  • 1
  • 17
  • 47
0
votes
0 answers

Can you use an object's instance address as the key in objc_setAssociatedObject?

Normally when using objc_setAssociatedObject, the recommended practice is to create a static variable, then use its address (via the '&' prefix) as the key. However, we have a case where we need to associate an arbitrary number of classes with one…
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286